Reading a local file on the iPhone simulator disappears, but not on a real device

I am developing an iphone application that downloads a file UTF8Encodedfrom the server, saves it in the application’s Cache folder and simply copies its contents to NSString* var. It works on the device, but not on the simulator, here is the code:

NSLog(@"File loaded into path: %@\n", localPath);
NSError* error;
NSString* tmpString = [NSString stringWithContentsOfFile:localPath encoding:NSUTF8StringEncoding error:&error];
NSLog(@"Error: %@", error);
//Prints the length in the console to check if the file has been correctly copied in the string
NSLog(@"tmpString length:%u", [tmpString length]);

On the simulator, this piece of code prints:

File loaded into path: /Users/username/Library/Application Support/iPhone Simulator/5.1/Applications/4FCF8FC6-4F1B-4FE5-92F6-A99EC8888E47/Library/Caches/utf8encodedFile.txt

But it crashes when it calls the " stringWithContentsOfFile:" method without displaying errors

Everything works well on a real device, it prints:

File loaded into path: /var/mobile/Applications/1EE8AEEB-D036-4ADE-AE12-836BA1F16BCB/Library/Caches/utf8encodedFile.txt
2012-06-30 19:19:24.743 appName[685:707] 
Error: (null)
2012-06-30 19:19:24.745 appName[685:707] tmpString length:1423
+5
source share
1 answer

Have you tried loading the contents of the file into NSData, and then creating NSStringwith it?

NSData dataWithContentsOfFile:

and NSString

- (id)initWithData:(NSData *)data encoding:(NSStringEncoding)encoding;
0
source

All Articles