MoralCode
MoralCode

Reputation: 2050

how to access a sound file stored in another app in swift?

this question is a lot like Share data between two or more iPhone applications except:

(this basically rules out using the share sheet and custom url's to pass data as described in the post above)

I am using AVAudioPlayer and AVAudioSession in the first app to play the sound if that is at all helpful.

Upvotes: 1

Views: 723

Answers (2)

SwiftArchitect
SwiftArchitect

Reputation: 48514

Use App Group

You can share actual NSData through the NSUserDefaults:

if let userDefaults = NSUserDefaults(suiteName: <group>) {
    userDefaults.setObject(obj, forKey: key)
}

and retrieve from another app in the same group like so:

if let userDefaults = NSUserDefaults(suiteName: <group>) {
    if let obj = userDefaults.objectForKey(key)  {
        // magic
    }
}

It appears that the only limitation for passing data through the user defaults is the device storage capacity, and since NSUserDefaults accepts NSData as a storage format, it makes a prime candidate for sharing tidbits information.

Upvotes: 2

Duncan C
Duncan C

Reputation: 131426

If both apps are yours you can implement a custom url scheme in the second app, and then from the first app ask if it knows how to open an URL with that scheme. If the answer is yes, the app is installed. The function is called canOpenURL. It's an instance method of UIApplication.

I vaguely remember that in iOS 9 and later, Apple added a restriction that you have to register the URLs you are going to ask about in your info.plist, but I don't remember the details. That won't prevent this scheme from working, but it is an extra step you have to take.

Upvotes: 0

Related Questions