I'm trying to implement a way to change language inside the application.
I think I have it all figured it out, but for some reason the folder is not laded as a bundle
I have a folder called kh.lproj
NSString *path = [[NSBundle mainBundle] pathForResource:newLanguage ofType:#"lproj"];
if ([[NSFileManager defaultManager] fileExistsAtPath:path]) {
bundle = [NSBundle bundleWithPath:path];
DLOG(#"Language: %# from path %# (%#)", newLanguage, path, bundle);
}
The output of my dlog is
Language: kh from path ......../F1AA1E74-A014-4331-BD1B-D05D0E54AFF3/console.app/kh.lproj
(NSBundle <......./F1AA1E74-A014-4331-BD1B-D05D0E54AFF3/console.app/kh.lproj>
(not yet loaded))
On both the iPhone and in the simulator (with different paths of course).
I have checked in the .app folder and the kh.lproj folder is there. (In lower case).
Does any one have an idea of why this is happening? If i try to load a file that doesn't exist it just ignores it and don't try to load it.
EDIT
If its to any help, when I'm using loadAndReturnError:(NSError) i get the message:
NSLocalizedFailureReason=The bundle’s executable couldn’t be located.,
NSLocalizedDescription=The bundle “kh.lproj” couldn’t be loaded
because its executable couldn’t be located.,
NSBundlePath=......./A65E8399-6CDB-4CAE-9074-803125E78BBA/storeconsole.app/kh.lproj
What does this error message mean
Looks like I get what I want anyway when calling
[bundle localizedStringForKey:key value:NULL table:nil];
a possible pitfall is also, when the string file has a bug, like a missing semicolon or incomplete comment marker.
Xcode dont check the file for syntax errors.
Related
While deleting existing the file with this command:
[[NSFileManager defaultManager] removeItemAtPath:self.sourceFileName error:&error];
I got the following error
Error: ImageIO: CGImageReadCreateDataWithMappedFile 'open' failed '/Users/asdasd/Library/Application Support/iPhone Simulator/7.1/Applications/DD251D7D-F0AF-40E1-A033-F221623D589D/Library/ScanSession/Source/page3.jpeg
error = 2 (No such file or directory)'
This happens while I copied pic from album into app folder. The most interesting thing is that file exists, but not fully copied. Is there a way to check wether file is file operation completed?
check weather your file & Directory available
for (NSString *filename in files) {
NSString *path = [yourPath stringByAppendingPathComponent:yourFileName];
BOOL isDir;
if([[NSFileManager defaultManager] fileExistsAtPath:yourPath isDirectory:&isAvilDir] && isAvilDir){
NSLog(#"%# Check is a directory", your path file);
}
else {
NSLog (#"%# Check is a file",your path file);
}
}
I have a similar problem right now, and it seems as though when you delete a file that certain other methods you may have called immediately prior may actually not have completed yet. I'm considering delaying the actual deletion of files to allow for background processes to complete.
Solved it 2 yars ago. Forgot to post & close the question. It was caused by another thread, where the file was deleted first. I think its one of the standart mutlithreading issues while working with CoreData
I want to copy file(image,mp3...) from main bundle to main bundle. To use
[[NSBundle mainBundle]pathForResource:#"copyname" ofType:#"mp3"]
from path = /var/mobile/Applications/62734FAF-1E94-4792-9978-exam/myproject.app/file.mp3
to path = /var/mobile/Applications/62734FAF-1E94-4792-9978-exam/myproject.app/copyname.mp3
function = copyItemAtPath
But i got the error "The operation couldn’t be completed. (Cocoa error 513.)"
Can anyone help me?
Bundles are read-only, it's not possible to write to them.
There are, however, many places where you can store your data.
You can use the Application Support Folder, or for temporary data you can also use the NSTemporaryDirectory() function to get a temporary directory.
This will also work in sandboxed applications.
Use
NSFileManager *fileManager = [NSFileManager defaultManager];
[fileManager copyItemAtPath:[[NSBundle mainBundle]pathForResource:#"FileNameToBeCopied" ofType:#"mp3"]
toPath:[[[NSBundle mainBundle]resourcePath]stringByAppendingPathComponent:#"NewFileName.mp3"] error:nil];
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.
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.
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.