How an app extension can access the contained app images / assets / resources

I have implemented a document provider app extension feature using my own ios app. The problem is that the app extension did not have access to the app containing images / assets or app resource data. Does anyone know how to achieve it?

+3


source to share


2 answers


The extension has its own purpose in your Xcode project. To access resources in an extension, you also need to add them to this target ( File Inspector

also check the extension target in the section Target Membership

). Just keep in mind that these resources are copied into the extension bundle as well, which increases the overall size of the application.



Sidenote: In my extension, I didn't add my default resource directory ( Images.xcassets

) to the extension, but still was able to access the contained images. Maybe this is an exception.

+4


source


UPDATE If you support iOS 8 and above, you can create a framework for sharing between your app and its extensions, and embed resources, strings, etc. Inside her. Be sure to use the [UIImage imageNamed:inBundle:compatibleWithTraitCollection:]

and methods NSLocalizedStringFromTableInBundle

when passing in the bundle, which is your framework.

PREVIOUS ANSWER Considering that extensions are (and hopefully always will be) in the main application subfolder at / Plugins / PluginName and have file permissions just like the main application, you can do the following (and I do in some of my applications in store):



// image
UIImage* image = [UIImage imageNamed:@"../../image_name.png"]

// data
NSString* path = [NSHomeDirectory() stringByAppendingPathComponent:@"../../file.json"];
NSData* data = [NSData dataWithContentsOfFile:path];

      

This will save you a lot of space, especially if your extension uses a lot of images.

+2


source







All Articles