Reputation: 1035
My application creates an NSData
from a song from the iTunes library. I wan't to write this to a file in the bundle ( the file doesn't exists ). How do I create a file there?
Upvotes: 2
Views: 2794
Reputation: 16725
stavash is right; you can't save to the bundle. To save to the documents directory, do something like this:
NSString *documentsDir = (NSString *)[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject];
NSString *pathForFile = [documentsDir stringByAppendingPathComponent:fileNameString];
[songData writeToFile:pathForFile atomically:YES];
And then to recover the contents of the file:
NSData *songData = [[NSData alloc] initWithContentsOfFile:pathForFile];
Upvotes: 4
Reputation: 14834
Just like stavash said, bundle is read only. You can save it to documents directory instead. Here is an example:
NSString *root = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0];
NSString *filePath = [root stringByAppendingPathComponent:@"yourfilename.ext"];
[yourFileData writeToFile:filePath atomically:NO];
Upvotes: 3
Reputation: 14304
You can't write to the bundle. Consider storing it in the documents directory.
Upvotes: 1