Skip to content
This repository has been archived by the owner on Jul 31, 2024. It is now read-only.

Using [NSBundle bundleForClass] gives bad path #107

Open
loretoparisi opened this issue Nov 3, 2014 · 1 comment
Open

Using [NSBundle bundleForClass] gives bad path #107

loretoparisi opened this issue Nov 3, 2014 · 1 comment

Comments

@loretoparisi
Copy link

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.

@jonathanpdiaz
Copy link

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;
}

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants