My Application is crashes in the device at this point,
[[NSBundle mainBundle] pathForResource
I am giving the path for the pdf here . but at the execution time it is showing that it is not getting path there . and that may be the reason for crashing.
however it is running perfectly on simulator .
i am not able to figure out why is this happening
here is that code
NSString *path = [[NSBundle mainBundle] pathForResource:[myArray objectAtIndex:0] ofType:[myArray objectAtIndex:1]];
NSLog(#"array elemt :%#", [myArray objectAtIndex:0]);
NSLog(#"array elemt 1 :%#", [myArray objectAtIndex:1]);
NSLog(#"path is :%#",path);
NSLog(#"responds to selector mainBundle=%#",[NSBundle respondsToSelector:#selector(mainBundle)]?#"YES""NO");
NSURL *targetURL = [NSURL fileURLWithPath:path];
in this log of the path - when i use device it is showing me nil and when i use simulator it is showing me the path . and it is showing me that is is crashing on NSURL line
I've found that when something like this works on the simulator, but not on the device, it's likely a problem with the case of the string. Your device is case sensitive but not your computer. Check your string of the file name or the type.
Related
So you know the part of the project that is "MyNameGoesHere.app" or "MyNameGoesHere.xcodeproj" - is there a way to get the MyNameGoesHere part via objective-c code?
I can get all kind of device info from UIDevice messages but I can't figure out how to get project/app name.
CFBundleDisplayName doesn't work anymore. Use:
NSString *bundleName = [[NSBundle mainBundle]
objectForInfoDictionaryKey:#"CFBundleName"];
You're probably looking at the bundle's InfoDictionary. You can get the app's name via the following code:
NSDictionary *info = [[NSBundle mainBundle] infoDictionary];
NSString *bundleName = [NSString stringWithFormat:#"%#", [info objectForKey:#"CFBundleDisplayName"]];
I want to put the content of my html resource file into an NSString object. Is it possible and advisable to do that? How could it be done?
Possible? - yes
Advisable? - unless it is an extremely large file, why not?
How? - There is already a method to do it for you in NSString - stringWithContentsOfFile:encoding:error:.
See the snippet below:
NSError* error = nil;
NSString *path = [[NSBundle mainBundle] pathForResource: #"foo" ofType: #"html"];
NSString *res = [NSString stringWithContentsOfFile: path encoding:NSUTF8StringEncoding error: &error];
I have a menu scene, help scene, settings scene and game scene. When i enter into game scene, it loads some data string from a .plist file into a NSMutableArray. I am doing this reading and loading procedure as follows-
+ (void) addObjectsToArray:(NSMutableArray*) mutableArray fromFile:(NSString*) filePath
{
NSString *bundle = [[NSBundle mainBundle] bundlePath];
NSString *path = [bundle stringByAppendingPathComponent:filePath];
[mutableArray addObjectsFromArray: [NSMutableArray arrayWithContentsOfFile:path]];
[bundle release];
}
I can get into the game scene and can back to menu scene from there.
But when I try to get into the game scene forth time(menu scene to game then back to menu scene and go to game scene again and do), the app just crashes.
I have found the crash point(using NSLog)-
+ (void) addObjectsToArray:(NSMutableArray*) mutableArray fromFile:(NSString*) filePath
{
NSString *bundle = [[NSBundle mainBundle] bundlePath];
**NSLog(#"always reach here");**
NSString *path = [bundle stringByAppendingPathComponent:filePath];**// CRASH POINT**
**NSLog(#"Forth time, doesnt reach here");**
[mutableArray addObjectsFromArray: [NSMutableArray arrayWithContentsOfFile:path]];
[bundle release];
}
But why crashing i dont understand and found no solution yet.
I think you should not release bundle
"[[NSBundle mainBundle] bundlePath]" should return an autorelease object.
Then as you release it, its relain count should reached 0 at the 4rd time and crash the app
You must not release an object if you don't own it.
You are responsible for (auto)releasing an object only if you increased its retain-count. Either by calling retain explicitly or by calling methods with alloc, copy or new in the name.
Please read the Memory Managment Guide again
Btw, if you run the analyzer (you can find it in the Build menu) it will point you at the exact line where you did something wrong.
Any stack trace?
Anyway try to wrap it into a try-catch. I always do this when handling files whether they are from bundle or downloaded.
#try {
NSString *bundle = [[NSBundle mainBundle] bundlePath];
NSString *path = [bundle stringByAppendingPathComponent:filePath];
[mutableArray addObjectsFromArray: [NSMutableArray arrayWithContentsOfFile:path]];
[bundle release];
}
#catch (NSException * e) {
NSLog (#"exception: %#", exception);
}
My guess is that filePath is nil.
Maybe try to NSLog(#"filePath: %#", filePath); in the beginning of (void) addObjectsToArray:(NSMutableArray*) mutableArray fromFile:(NSString*) filePath
Cheers mate.
I am reading a file in iphone app. Its Works Fine.
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSLog(#"%#",documentsDirectory);
NSString *fileName = [NSString stringWithFormat:#"%#/test.txt",documentsDirectory];
NSLog(#"%#",fileName);
NSString *content = [[NSString alloc] initWithContentsOfFile:fileName
usedEncoding:nil
error:nil];
NSLog (#"%#",content);
}
works good when i am using filename as test.txt
But when i add another file in the resource suppose test1.txt then NSLog(#"%#",documentsDirectory) and NSLog(#"%#",fileName) shows the right result. But
NSLog (#"%#",content); prints null in the log. So what is the reason?
I am printing detail error and it prints
NSFilePath = "/Users/sam-xxx/Library/Application Support/iPhone Simulator/3.2/Applications/51197946-6042-4A90-AA39-F07F8A649308/Documents/test1.txt";
NSUnderlyingError = Error Domain=NSPOSIXErrorDomain Code=2 "Operation could not be completed. No such file or directory";
It would be best here to check to see if an error is returned:
NSError *error;
NSString *content = [[NSString alloc] initWithContentsOfFile:fileName
usedEncoding:nil
error: &error];
if (error) NSLog(#"Error !: %#", [error localizedDescription]);
That will (hopefully) give you a clue as to whats going on.
(Edited to give example bundle resource usage as the file is in the bundle not the Documents directory).
Docs for NSBundle are here: NSBundle Documentation
You have 2 choices, the one you suggest:
NSString *databasePathFromApp = [[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent:fileName];
Which will return the Bundle resource directory with the filename appended to that path.
Personally I prefer the pathForResource:ofType: method:
NSString *filename = [[NSBundle mainBundle] pathForResource: #"test1" ofType: #"txt"];
As this will not only tell you if the file exists (returns nil if it does not) but will also search the localisation directories if you have them.
Did you check if the file in the error message exists? When run in the simulator you can simply open Finder, press cmd+shift+g and paste the directory to the file.
Maybe you didn't add it to the target? Open the information window for the file in your resources and check which targets are checked.
When you run a app in Simulator, you could copy and paste the right file into the app's Documents directory use Finder, but on iPhone or iTouch how could you paste a file into the Documents directory.
We got only 2 method to put something into Documents directory when app is running on iPhone or iTouch.
Create one.
Copy one from the bundle.
in your code, you read a file in the Documents directory, you could be paste a file in the directory manually use Finder. Try this.
NSString *path = [[NSBundle mainBundle] pathForResource:#"test.txt" ofType:nil];
NSString *content = [[NSString alloc] initWithContentsOfFile:path
usedEncoding:nil
error:nil];
NSLog (#"%#",content);
Is there some difference between the way that bundle resources can be accessed on the iPhone simulator versus a device (in my case, an iPad)? I am able to access a file on the former, but not the latter.
NSString *filePath = [NSString stringWithFormat:#"%#%#",[[NSBundle mainBundle] bundlePath], #"/AppResources/html/pages/quickHelp.html"];
BOOL fileExists = [[NSFileManager defaultManager] fileExistsAtPath:filePath];
// fileExists == YES in the debugger on both the simulator and the device
NSString *path = [NSString stringWithFormat:#"AppResources/html/pages/%#", contentsFileName];
NSString *pathForURL = [[NSBundle mainBundle] pathForResource:path ofType:#"html"];
NSURL *url = [NSURL fileURLWithPath:pathForURL isDirectory:NO];
The code above works fine in the simulator, but on the device pathForResource:path returns nil, so the last line throws a 'nil string parameter' exception.
Am I missing something obvious?
edit: Of course, in the above #"quickHelp" is being passed in the contentsFileName var.
edit2: if it makes any difference, in my build settings "Base SDK" is set to "iPhone Device 4.0", and "iPhone OS Deployment Target" is set to "iPhone OS 3.1.3". Any other settings that might have an influence?
[ignore]
I notice a leading '/' on your *filePath but none exists on *path (before AppResources)
[/ignore]
[edit]
[[NSBundle mainBundle] pathForResource:#"quickHelp" ofType:#"html" inDirectory:#"AppResources/html/pages"]]
[/edit]
I believe the filesystem on the iPhone is case-sensitive. Check your cases with the actual files.