-1

am working over an iOS App and am having a huge bad time over this issue, here is the thing:

1.- Am creating a file based on some NSData 2.- Am trying to retrieve the NSURL of that new file that i just created

the issue is that the NSURL doesn't seems to find the file, which i have attempt to create with two different methods

NSdata * fileData = /*Gets the data of the file from the web*/

if([fileData writeToFile:databasePath atomically:YES])
{

    NSLog(@"path to resource :%@", [[[NSBundle mainBundle] pathForResource:@"myTMP"  ofType:@"epub"] description]);
    //This causes an exception cause the string is Nil but means that the file is created
    return [NSURL fileURLWithPath:[[NSBundle mainBundle] pathForResource:@"myTMP" ofType:@"epub"]];

}

if i try this:

NSString* path=@"myTMP.epub";
[fileData writeToFile:path options:NSDataWritingAtomic error:&error];
NSLog(@"Write returned error: %@", [error localizedDescription]);

it never creates the new file, any help, suggestions, comments will be very appreciated best regards,

Jorge.

1 Answers1

0

You are not allowed to write into the application bundle, for obvious security reasons. You'll need to write into the Documents or other application bundle folder. Use NSFileManager's URLsForDirectory:inDomains: method as documented here:

http://developer.apple.com/library/ios/DOCUMENTATION/FileManagement/Conceptual/FileSystemProgrammingGuide/AccessingFilesandDirectories/AccessingFilesandDirectories.html#//apple_ref/doc/uid/TP40010672-CH3-SW3

See http://developer.apple.com/library/ios/DOCUMENTATION/FileManagement/Conceptual/FileSystemProgrammingGuide/FileSystemOverview/FileSystemOverview.html#//apple_ref/doc/uid/TP40010672-CH2-SW2 for more general information relating to reading & writing files on iOS.

Dad
  • 5,473
  • 1
  • 24
  • 32