To access the containing app’s image, asset, or resource data from the app extension, you can use the NSItemProvider
API. The NSItemProvider
is used to vend data between processes and can be used from both the containing app and its extensions.
Here is an example of how to access an image asset from the containing app in a Swift app extension:
main.swift267 chars7 lines
In the above example, we create an NSItemProvider
object with the image asset and set its typeIdentifier
to the kUTTypeImage
constant to indicate that it is an image. We then create an NSExtensionItem
object and attach the NSItemProvider
instance to it as an attachment. This NSExtensionItem
instance can now be returned from the app extension to the containing app.
It is important to note that the app extension needs to share an App Group with the containing app to access its data. You can set up an App Group from your Xcode project’s capabilities tab. You also need to add the App Group identifier to both the containing app and the extension’s entitlements file. Once the app group is set up and entitlements file is set up, you can use the NSUserDefaults
API to store and retrieve data between your extension and your containing app.
gistlibby LogSnag