iOS-Framework
iOS-Framework copied to clipboard
Using [NSBundle bundleForClass] gives bad path
Within a well configured iOS-Framework if I try to load file resources
NSBundle *localBundle = [NSBundle bundleForClass:[myObject class]]
gives back the main app bundle i.e. the same bundle of
NSBundle *mainBundle = [NSBundle mainBundle]
that is the container app root bundle folder like
NSBundle </private/var/mobile/Containers/Bundle/Application/C95A7E51-2871-4489-B448-025CAFDE1B4C/MXMTestbed.app> (loaded)
My container app and framework structure is like
MyContainerApp
|------Frameworks/
|-----------------MyFramework.framwork/
|-------------------Resources/
|-------------mydata.json
and I want to grant access in the framework (not in the app code) for mydata.json. It's very easy to access same data files via iOS-Framework Bundles, but this is not the case.
In fact the method to get the Framework bundle like:
+ (NSBundle *)frameworkBundle {
static NSBundle* frameworkBundle = nil;
static dispatch_once_t predicate;
dispatch_once(&predicate, ^{
NSString* mainBundlePath = [[NSBundle mainBundle] resourcePath];
NSString* frameworkBundlePath = [mainBundlePath stringByAppendingPathComponent:@"ObjectiveLyricsTouchResources.bundle"];
frameworkBundle = [NSBundle bundleWithPath:frameworkBundlePath];
});
return frameworkBundle;
}
will work only when calling it from the container app, not in the framework itself, due to the issue showed before.
It worked for me in the framework doing this: I think the secret is use [self class]
[[[self class] frameworkBundle] pathForResource:@"file" ofType:@"cer"];
+ (NSBundle *)frameworkBundle {
static NSBundle* frameworkBundle = nil;
static dispatch_once_t predicate;
dispatch_once(&predicate, ^{
NSString* mainBundlePath = [[NSBundle mainBundle] resourcePath];
NSString* frameworkBundlePath = [mainBundlePath stringByAppendingPathComponent:@"Bundle-iOS-SDK.bundle"];
frameworkBundle = [NSBundle bundleWithPath:frameworkBundlePath];
});
return frameworkBundle;
}