NSBundle creates files for given path - ios

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.

Related

App localization showing the key instead of the value in iOS

I've been using localization in my app, but for some reason, some of the strings (not all of them) won't translate, I see the key instead the value. I've tried to check if the app finds the localization files by doing this:
NSString *enPath = [[NSBundle mainBundle] pathForResource:#"en" ofType:#"lproj"];
NSString *hePath = [[NSBundle mainBundle] pathForResource:#"he" ofType:#"lproj"];
NSString *ruPath = [[NSBundle mainBundle] pathForResource:#"ru" ofType:#"lproj"];
NSString *esPath = [[NSBundle mainBundle] pathForResource:#"es" ofType:#"lproj"];
NSString *frPath = [[NSBundle mainBundle] pathForResource:#"fr" ofType:#"lproj"];
NSString *arPath = [[NSBundle mainBundle] pathForResource:#"ar" ofType:#"lproj"];
And none of them is nil.
I've checked the name of the localization file and it's Localizable.strings as it should be.
Also checked if the key exists inside the Localizable.strings files and it does.
I've also tried:
Empty Cache
Cleaning all targets
Delete Derived Data folder
Restart
Reset simulator
Convert to UTF-16
Remove all localization files and recreate them.
Also tried to do everything that is in this question.
It's important to say that this is not just a Simulator/Cache problem. It's also showing on devices which download the app. (I have Enterprise account).
What more can I do in order to identify nor fix the problem?
So I found the problem, I guess who translated the Localizable.strings files for me is an asshole. In 4 places in my strings file there was a row as followed:
"KEY" ;= "Value"
This line cause some kind of a crash, but let the compiler to build successfully for some reason. That's why I couldn't find the bug, only when I decided to take the last Key and Value which are not translate and move them to the top of the Localizable.strings file. Then I was able to understand and see that the problem is somewhere in the middle of the file and the top Keys and Values are translated fine.
One thing that you can do catch these kind of errors is to make a copy of the strings file, change the extension to plist and try to open it in Xcode. If there is any problem in the strings file it will show in Xcode since the dictionary will contain only the keys till the point where there is an error. You can then do a Find operation and find the error until you are sure that all strings appear in the plist file. You can then rename the file back to .strings
If you specify table:nil, then NSBundle will try to fetch the localization from the default table (the one in SOMELANG.lproj/Localizable.strings). If you have the localization elsewhere, you should explicitly specify the table using table:#"File" (or use the NSLocalizedStringFromTable() macro in a similar manner:
NSString *value = NSLocalizedStringFromTable(#"key", #"File", nil);
Also,
Double check that the Localizable.strings file is being added to
Targets -> BuildPhases -> Copy Bundle Resources
It hadn't been added automatically for me.

pathForDirectory method for NSBundle?

If I include a folder in my bundle (a real folder, the blue ones, not the yellow groups), how can I get the path for that folder from my bundle? The method I would usually use is...
[[NSBundle mainBundle] pathForResource:fileName ofType:______];
...but what "type" is a directory? Is there a "type" to use, or is there another method for accessing the paths of directories within the bundle?
Or am I going about this all wrong, and there's some other way for including folders of accessible documents in the bundle?
Directories can have extensions too. If yours doesn't have one, just pass #"" for the type parameter, -[NSBundle pathForResource:ofType:] works for directories too, not only files. At the end, a directory is also a resource :)
As per a suggestion in the comments, it turns out you can use an empty string to refer to folders. So a folder called "myFolder" would be accessible using...
[[NSBundle mainBundle] pathForResource:#"myFolder" ofType:#""];
Hope this helps anyone else who wasn't expecting it to be so easy...
As you're adding a folder by your own, you know the name of the folder, so you can get the path like this:
NSString *myDirectoryName = #"myDirectory";
NSString *absolutePathToMyDirectory = [[NSBundle mainBundle].resourcePath stringByAppendingPathComponent:myDirectoryName];
You'll have to test if this path exist with NSFileManager.
Otherwise you can still use the method you're using, the type can be nil, so you can call it like this
NSString *absolutePathToMyDirectory = [[NSBundle mainBundle] pathForResource:myDirectoryName ofType:nil];
and then testing if the result is nil or not.
if(absolutePathToMyDirectory) {
// do stuff related to this path
}

pathForResource is returning null?

I copied a mp4 format file into my XCode. named as abc.mp4
NSString *filePath = [[NSBundle mainBundle] pathForResource:#"abc" ofType:#"mp4"];
As I log filePath, it return me null.
How can I access my XCode resources files?
You probably have not checked the box to include that file. What you show is an attempt to fetch the file "abc.mp4" from your bundle.
Look in the target's build phases, and make sure that file is being copied to the bundle.

How can I get full path of a directory (not a file) in developing ios

I am using cocos2d-x and I found it used pathForResource to get full path for a existing file. But I need to get the full path of a directory. If I simply pass "" to pathForResource, it will search and return the first file in that directory.
I cannot just take application directory and put it in front of relative path since I don't know if it's a relative one or already a full one.
I can trim the filename but I think that's a weird solution.
So is there any function in objective-c that works like pathForResource but don't really search for files... just return the directory name
BTW, I am using opendir functions in dirent.h. I found it won't work if I just pass a relative path, which was fine under windows.
NSString *sourcePath = [[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent:#"DirectoryName"];
NSLog(#"%#", sourcePath);

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