To share code and resources between my app and extensions, I embed a Cocoa Touch Framework
in my project, this framework contains some resources like image and xib files.
My question is: how do I access those resources from may main app and extensions?
I used to use this method: iOS 8 Extension: how to use xib inside Cocoa Touch Framework in extensions?
but this will make a copy to all resources in app's bundle and extensions' bundle , which is not a good repeat.
Is there a better way?
Update:
Now my main app can use code like this to access resource in frameworks:
+(NSBundle*)frameworkBundle
{
NSString *frameworkDirPath = [[NSBundle mainBundle] privateFrameworksPath];
NSString *frameworkBundlePath = [frameworkDirPath stringByAppendingPathComponent:@"MyFramework.framework"];
NSBundle *frameworkBundle = [NSBundle bundleWithPath:frameworkBundlePath];
return frameworkBundle;
}
but this doesn't work for extensions.
试试这个:
NSBundle *frameworkBundle = [NSBundle bundleForClass:[AnyClassFromFramework class]];
这对我有用:
NSBundle * frameworkBundle = [NSBundle bundleWithIdentifier:@"<framework-bundle-identifier>"];
对于swift ,请考虑使用:
let bundle = NSBundle(forClass: self.dynamicType)
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.