Is there anyway to do it?
I've tried pathForResource:ofType: on NSBundle and it can't find the xib when it's really there. If I try to load it and it doesn't exist loadNibNamed:owner:options: crashes and so does nibWithNibName:bundle:
Basically, I want to know at runtime whether I should register a xib or just a class for a given cell type on a collection view.
Are you sure pathForResource:ofType: is not working? It's not just the common xib / nib confusion?
(.xib-files are ofType:#"nib")...
NSBundle *mainBundle = [NSBundle mainBundle];
NSString *path = [mainBundle pathForResource:#"crapXib" ofType:#"nib"]; // file is called crapXib.xib
if (path){
// whatever...
} else{
// whatever else
}
You can try to use NSFileManager to check if file exists, if application is not localized all .nibs live in root directory of .ipa, for example:
NSString * root = [[NSBundle mainBundle] resourcePath];
if ([[NSFileManager defaultManager] fileExistsAtPath: [root stringByAppendingPathComponent:#"MainViewController.nib"]])
{
// exisits
}
Related
I had create a framework that send data to server. Whenever I get response from the server I translate it using NSLocalizedString but it not working.
I tried to change Main Bundle to my Framework Bundle like this :
NSString* mainBundlePath = [[NSBundle mainBundle] resourcePath];
NSString* frameworkBundlePath = [mainBundlePath stringByAppendingPathComponent:#"Frameworks/MYFRAMEWORK.framework"];
NSBundle *bundle = [NSBundle bundleWithPath:frameworkBundlePath];
[bundle localizedStringForKey:#"Message" value:#"" table:nil];
But still not working. is there any way to localized message when the Localizable.string is inside the framework ?
Thanks
Finally, I tried to recreate Localizable.string and whenever I want to localize I use this :
NSString *message = [[NSBundle bundleForClass:self.class] localizedStringForKey:#"MESSAGE" value:nil table:nil];
I put this code inside class of framework, so bundle of self.class is direct to framework bundle. Thank's for everybody help.
Try to use [NSBundle bundleForClass:self.class].
I am trying to load a .xib based on the language selected by the user at the runtime.
To get the particular Bundle I use the following code
-(NSBundle*) getMyBundle{
NSArray *languages=[NSLocale preferredLanguages];
NSString *basePath=[[NSBundle mainBundle] pathForResource:#"Base" ofType:#"lproj"];
if(languages.count){
NSString *langKey=[languages objectAtIndex:0];
if(![util checkIfStringIsEmpty:langKey]){
NSString *path=[[NSBundle mainBundle] pathForResource:langKey ofType:#"lproj"];
if(![util checkIfStringIsEmpty:path]){
return [NSBundle bundleWithPath:path];;
}
}
}
return [NSBundle bundleWithPath:basePath];
}
-(void) loadMyController{
AViewController *controller=[[AViewController alloc] initWithNibName:#"AViewController" bundle:[self getMyBundle]];
[self.navigationController pushViewController:controller animated:YES];
}
So, with above code .xib are loaded correctly , but since images are cached, they are not picked up.
So, any solution to overcome this issue will be a great help.
A little embarrassing question, but I can find an answer which works in my case... I need to put some xml file (settings.xml) in order to read some data from it during application runtime.
According to some answers here and not only here, I have putted it here:
~/Library/Application Support/iPhone Simulator/5.0/[AppUUID]/Documents
and I'm trying to use it as follows:
// Loading data from external XML File
NSURL *url = [[NSBundle mainBundle]
URLForResource: #"settings" withExtension:#"xml"];
NSError *err;
if ([url checkResourceIsReachableAndReturnError:&err] == NO){
NSLog(#"FILE NOT FOUND");
}
Result: "FILE NOT FOUND".
I've tried to do put the file under any possible directory in
~/Library/Application Support/iPhone Simulator/5.0/[AppUUID]/ and efect is still the same.
I'm using XCode 4.2
If you are putting the file into the .../Documents folder then you need to use the following code to access it (you are looking for it in the App Bundle, which is a different location altogether):
NSString *docFolder = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0];
NSString *filename = [docFolder stringByAppendingPathComponent:#"settings.xml"];
if ([[NSFileManager defaultManager] fileExistsAtPath:filename])
{
// Read file
}
else
{
NSLog(#"settings.xml file not found!");
}
I want to access data (xml, to be specific) in different filepaths, depending on some parameters. In short, theres a /Saves/ folder, which contains several other folders (/Save1/, /Save2/, /Save3/, etc). I want to be able to parse.
Lets assume the file I want to access is in the following path:
/../projectname/Saves/Save1/dialogue.xml
Within finder I created the folder substructure and then copied it into my XCode project. That means, these folders exist in the filestructure and not just in XCode.
Afterwards I'm trying to do the following:
NSString *path = [[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent:"#/Saves/Save1/dialogue.xml"];
NSData *xmlData = [[NSMutableData alloc] initWithContentsOfFile:path];
However, that doesn't seem to work properly. Logging the path variable I get the following:
/Users/<username>/Library/Application Support/iPhone Simulator/5.1/Application/<appcode>/<appname>.app/Saves/Save1/dialogue.xml
Where's my mistake? Thanks in advance for any help.
Please make sure you have added same folder in project target also. One you add the same structure under project target and compile then same folders will be created in your .app file. then you can access it the way you mentioned.
try this:
//NSString *path = [[NSBundle mainBundle] pathForResource:#"dialogue" ofType:#"xml" inDirectory:#"Saves/Save1"];
NSString *path = [[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent:#"Saves/Save1/dialogue.xml"];
if([[NSFileManager defaultManager] fileExitsAtPath:path])
{
NSMutableData *xmlData = [[NSMutableData alloc] initWithContentsOfFile:path];
}
else
{
NSLog(#"File doesnot exits");
}
I am new to iPhone programming. I want to read the content of a text file located in a subfolder of the Resource folder.
The Resource folder structure is the following:
Resource
Folder1---->Data.txt
Folder2---->Data.txt
Folder3---->Folder1---->Data.txt
There are multiple files named "Data.txt", so how can I access the files in each folder? I know how to read the text file, but if the Resource structure is similar to the above structure then how can I get the path?
For example, if I want to access the "Data.txt" file from Folder3, how can I get the file path?
Please suggest.
Your "resource folder" is actually the contents of your main bundle, also know as the application bundle. You use pathForResource:ofType: or pathForResource:ofType:inDirectory: to get the full path for a resource.
Loading the contents of a file as a string is done with the stringWithContentsOfFile:encoding:error: method for an autoreleased string of with initWithContentsOfFile:encoding:error: if you want a retained string.
NSString *filePath = [[NSBundle mainBundle] pathForResource:#"Data"
ofType:#"txt"
inDirectory:#"Folder1"];
if (filePath != nil) {
theContents = [NSString stringWithContentsOfFile:filePath
encoding:NSUTF8StringEncoding
error:NULL];
// Do stuff to theContents
}
This is almost the same answer as given by Shirkrin previously, but with the slight difference that it works on target. This is because initWithContentsOfFile: is deprecated on Mac OS X, and not available at all iPhone OS.
To continue psychotiks answer a full example would look like this:
NSBundle *thisBundle = [NSBundle bundleForClass:[self class]];
NSString *filePath = nil;
if (filePath = [thisBundle pathForResource:#"Data" ofType:#"txt" inDirectory:#"Folder1"]) {
theContents = [[NSString alloc] initWithContentsOfFile:filePath];
// when completed, it is the developer's responsibility to release theContents
}
Notice that you can use -pathForResource:ofType:inDirectory to access ressources in sub directories.
Shirkrin's answer and PeyloW's answer above were both useful, and I managed to use pathForResource:ofType:inDirectory: to access files with the same name in different folders in my app bundle.
I also found an alternative solution here that suited my requirements slightly better, so I thought I'd share it. In particular, see this link.
For example, say I have the following Folder References (blue icons, Groups are yellow):
Then I can access the image files like this:
NSString * filePath = [[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent:#"pin_images/1/2.jpg"];
UIImage * image = [UIImage imageWithContentsOfFile:filePath];
As a side note, the pathForResource:ofType:inDirectory: equivalent looks like this:
NSString * filePath = [[NSBundle mainBundle] pathForResource:#"2" ofType:#"jpg" inDirectory:#"pin_images/1/"];
NSBundle* bundle = [NSBundle mainBundle];
NSString* path = [bundle bundlePath];
This gives you the path to your bundle. From there on, you can navigate your folder structure.