bundlePath is nil but NSLog prints right path - ios

I try to get resource from bundle inside framework. As you can see bundlePath is nil. But NSLog prints right path. And bundle is nil. Why?

Not sure why it's happening, but the code is inefficient given you get a bundle in order to allocate a new one. This is clearly unnecessary:
NSBundle *bundle = [NSBundle mainBundle];
NSURL *modelURL = [bundle URLForResource:... ];
I missed the framework-element when answering this question. As you are calling this code from within the framework code I assume that [NSBundle mainBundle] is returning the bundle of the framework and not the app.
The answer lies in the app providing the framework code with the app's bundle via some method. The framework code should then be able to access files within the app bundle.

Related

NSBundle pathForResource doesn't work for subdirectory localization?

I saw quite a few questions regarding [NSBundle MainBundle] pathForResource (using inDirectory or not), but my case seems different.
My problem is: Yes, it works fine for whatever files in subdirectory if without localization. For example, it returns the correct path for the file data/a/words.txt when I use
[[NSBundle mainBundle] pathForResource:#"words.txt" ofType:nil inDirectory:#"data/a"]
However, after I localized the words.txt, let's say the real path becomes: data/a/en.lproj/words.txt, then the above code cannot find the path anymore.
I checked the file in the .app package and the file has been copied into the correct path (data/a/en.lproj), it's just somehow the code cannot find it.
I'm using XCode 5.1.1
Isn't pathForResource supposed to find the text automatically?
Try adding forLocalization to pathForResource, like this:
NSArray* availableLocalizations = [[NSBundle mainBundle] localizations];
NSArray* userPrefered = [NSBundle preferredLocalizationsFromArray:availableLocalizations forPreferences:[NSLocale preferredLanguages]];
NSString *indexPath = [[NSBundle mainBundle] pathForResource:#"words" ofType:#"txt" inDirectory:#"data/a" forLocalization:[userPrefered objectAtIndex:0]];

iOS Extension and accessing files in Resource

Suppose I have a file data.txt in the Resource bundle of an extension (not the Resource bundle of the application). How do I access it? I have tried:
NSString *filePath = [[NSBundle mainBundle] pathForResource:#"data" ofType:#"txt"];
But it returns nil.
It turns out all I need to do is to add this file to the Application's target, and not only the Extension's target.
Add your resource file to main app bundle which contains the extension as embedded binary and then call this method. It will return correct path.

NSBundle creates files for given path

When I use
NSString *path = [[NSBundle mainBundle] pathForResource:name ofType:#"sqlite"];
The NSBundle class, instead of returning nil, for a given file name when the file is not included in Xcode, seems to be creating the file no matter what. I tried removing file from xcode, it did not work, then I went to the path for simulator for the project (that path is generated by pathForResource) :
/Users/USER_NAME/Library/Application
Support/iPhoneSimulator/6.0/Applications/APP_CODE/APP_NAME.app/sampleDB.sqlite
Before I run program in the debugger, I delete file "sampleDB.sqlite" manually. Every time the method pathForResource is called the file seems to magically reappear in the folder.
The documentation states :
Return type :
The full pathname for the resource file or nil if the file could not be located.
EDIT:
With a pathForResource commented out, files removed - I still get that file magically reappearing in the bundle. Any Ideas?
Make a clean build to make sure that the file gets removed from the app bundle.

NSBundle - (not loaded yet) Error

I am trying to get a strings file table for use with NSLocalizedStringFromTableInBundle.
I am using this method:
+(NSBundle*)getBundleForLang:(NSString*)lang{
//get the path to the bundle
NSString *bundlePath = [[NSBundle mainBundle] pathForResource:#"localizable" ofType:#"strings" inDirectory:nil forLocalization:lang];
NSLog(#"bundlePath = %#",bundlePath);
//load the bundle
NSBundle *langBundle = [[NSBundle alloc] initWithPath:[bundlePath stringByDeletingLastPathComponent]];
NSLog(#"langBundle = %#",langBundle);
return langBundle;
}
While it is working great on the simulator, when i try to use it on an iPhone device i get this NSLog:
2011-12-09 00:40:14.533 MyApp[12754:707] langBundle = NSBundle
</var/mobile/Applications/915E6BCB-EC44-4F1D-891B-EF68E2FA89C2/MyApp.app/he.lproj>
(not yet loaded)
Why isn't it loaded and where is the problem?
Thanks
Shani
Check the case of your file paths.
The simulator (by default) is not case sensitive, whereas the device is.
This could cause the simulator to successfully find the file, but the device to fail.
It's not an error. Strings bundle, such as en.lproj, does not include executable file. When you try to [bundle loadAndReturnError:], it will fail, and loadAndReturnError:'s document will tell you why.
Make sure the bundle contains a bundle identifier.
If it does not, despite the path is available, the resources can't load from there.

URLForResource:withExtension: method always returns nil

I am testing the MixerHost sample code. However the following code:
NSURL *beatsLoop = [[NSBundle mainBundle] URLForResource: #"beatsMono"
withExtension: #"caf"];
the beatsLoop is nil.
What's the reason for that?
Should I first create the beatsMono.caf file and then put into some specific path?
Any comments and solutions will be highly appreciated.
Thanks,
finspoo
Should I first create the beatsMono.caf file and then put into some specific path?
Yes. The file beatsMono.caf (case sensitive!) must exist in your application bundle for that method to succeed. This is done by adding the file to the target as a resource in XCode, you cannot do it at runtime.

Resources