Duck
Duck

Reputation: 36003

Bundle from Static LIbrary

I have added a subproject as a static library to my main project.

Now, on the main project I am trying to load something that is on the bundle of the subproject. Something like this:

NSString *defaultStorePath =
[[NSBundle bundleForClass:[self class]] pathForResource:@"database" ofType:@"sqlite"];

but this is returning nil...

how do I solve that?

Upvotes: 3

Views: 3182

Answers (3)

Leszek Szary
Leszek Szary

Reputation: 10346

I had similar problem and in my case even bundleWithIdentifier returned nil for some reason. If nothing else works you can always try to iterate over all files like below. That worked for me.

[self urlForResource:@"database.sqlite"];

// ...

- (NSURL *)urlForResource:(NSString *)resource
{
    NSFileManager *fileManager = [NSFileManager defaultManager];
    NSURL *directoryURL = [NSBundle bundleForClass:[self class]].bundleURL;
    NSDirectoryEnumerator *enumerator = [fileManager enumeratorAtURL:directoryURL includingPropertiesForKeys:nil options:0 errorHandler:^BOOL(NSURL *url, NSError *error) {
        return YES;
    }];

    for (NSURL *url in enumerator)
    {
        NSString *lastPathComponent = [[url absoluteString] lastPathComponent];
        if ([lastPathComponent isEqual:resource])
            return url;
    }
    return nil;
}

Upvotes: 0

Duck
Duck

Reputation: 36003

A-live gave the answer, but he is too modest to post it here as an answer.

Here is the link with the answer http://www.galloway.me.uk/tutorials/ios-library-with-resources/

The problem was exactly because resources are not included when you create a static library.

Upvotes: 4

Anand Gautam
Anand Gautam

Reputation: 2579

Try something like this..

    NSString *resourceBundlePath = [[NSBundle mainBundle]       
                   pathForResource:@"StaticLibrary" 
                                       ofType:@"bundle"];

    NSBundle *resourceBundle = [NSBundle bundleWithPath:resourceBundlePath];

Now, you can use this resourceBundle as your static Library Bundle.

Upvotes: 1

Related Questions