2

I am getting various format of documents with extension as .png,.txt,.jpg,,mp3 etc as NSData formatt, I need to store these locally and view it ,I have tried this

 NSData* conData = [convStr dataUsingEncoding:NSUTF8StringEncoding];
NSLog(@"the converted string to nsdata is :%@",conData);

[conData writeToFile:@"Filename.txt" atomically:YES];

but unfortunately this is not working can any one help.

Arun Gupta
  • 2,628
  • 1
  • 20
  • 37
jerfin
  • 803
  • 1
  • 13
  • 28
  • Possible duplicate of [How do I download and save a file locally on iOS using objective C?](http://stackoverflow.com/questions/5323427/how-do-i-download-and-save-a-file-locally-on-ios-using-objective-c) – Arun Gupta Apr 02 '16 at 06:20
  • You have give the directory where to save the file documents, library, cache... You are missing that particular code. – Arun Gupta Apr 02 '16 at 06:22
  • @ arun Gupta can you help me with the code. – jerfin Apr 02 '16 at 06:27
  • Which part are you facing the issue. Didn't the above url helped. Can you please post the entire code and what issue you are facing. Are you able to see the file in the documents directory? – Arun Gupta Apr 02 '16 at 06:34

3 Answers3

1

Note - writing NSData into a file is an IO operation which may block your main thread use dispatch, also provide file path for writing instead file name only.

This writes file for me

dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_BACKGROUND, 0), ^{
    // Generate the file path
    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES);
    NSString *documentsDirectory = [paths objectAtIndex:0];
    NSString *dataPath = [documentsDirectory stringByAppendingPathComponent:@"Filename.txt"];

     // Save it into file system
    [data writeToFile:dataPath atomically:YES];
});
swiftBoy
  • 35,607
  • 26
  • 136
  • 135
  • NSString *dataPath = [documentsDirectory stringByAppendingPathComponent:@"Filename.txt"]; if i use this can i able to save files with .txt extensions or all other formats such as .jpg,.pdf,.png.mp3 etc – jerfin Apr 02 '16 at 06:33
  • @LeoDabus his original code is in **objective-c** , which he posted in question – swiftBoy Apr 02 '16 at 06:38
  • 1
    @Arun Yes you can save any type of file.The best approach would be to create a common method and pass them paramater as NSData and fileName. Using fileName you can also get the extension to change the implementation for particular filetype and using the filename it will save the file with desired extension. – Arun Gupta Apr 02 '16 at 08:18
1

you should pass a correct path in writeToFile method

NSString *docsDir;
NSArray *dirPaths;

dirPaths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
docsDir = [dirPaths objectAtIndex:0];
NSString *targetPatch = [[NSString alloc] initWithString: [docsDir stringByAppendingPathComponent:@"Filename.text"]];
[data writeToFile:targetPatch atomically:YES];
Mo Farhand
  • 1,144
  • 8
  • 21
0

You have given only file name(Filename.txt) not whole file path thats why your code is not working.

Check below line of code you will get idea.

 NSArray *paths =  NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,     NSUserDomainMask, YES);
NSString  *documentsDirectory = [paths objectAtIndex:0];  

NSString  *filePath = [NSString stringWithFormat:@"%@/%@", documentsDirectory,@"Filename.txt"];
[urlData writeToFile:filePath atomically:YES];
Payal Maniyar
  • 4,293
  • 3
  • 25
  • 51