IOS 8: get resource from Cocoa Touch Framework

To share code and resources between my application and extensions, I embed the Cocoa Touch Framework in my project, this environment contains some resources, such as images and xib files.

My question is: how do I access these resources from the main application and main extensions?

I used this method: iOS 8 Extension: how to use xib inside Cocoa Touch Framework in extensions?

but it will make a copy for all the resources in the bundle of applications and extensions , which is not a good repetition.

Is there a better way?

Update:

Now my main application can use this code to access the resource in the frameworks:

 +(NSBundle*)frameworkBundle { NSString *frameworkDirPath = [[NSBundle mainBundle] privateFrameworksPath]; NSString *frameworkBundlePath = [frameworkDirPath stringByAppendingPathComponent:@"MyFramework.framework"]; NSBundle *frameworkBundle = [NSBundle bundleWithPath:frameworkBundlePath]; return frameworkBundle; } 

but this does not work for extensions.

+5
source share
3 answers

Try the following:

 NSBundle *frameworkBundle = [NSBundle bundleForClass:[AnyClassFromFramework class]]; 
+15
source

This worked for me:

 NSBundle * frameworkBundle = [NSBundle bundleWithIdentifier:@"<framework-bundle-identifier>"]; 
+5
source

For a quick look, consider using:

 let bundle = NSBundle(forClass: self.dynamicType) 
0
source

Source: https://habr.com/ru/post/1205002/


All Articles