1

I have a scenario in my application that I need to download mp3, pdf, text files using my application.

How can I download, and where to store, data using my application?

Anthon
  • 69,918
  • 32
  • 186
  • 246
lakshmi
  • 59
  • 1
  • 3

3 Answers3

1
NSString *stringURL = @"http://www.somewhere.com/thefile.png";
NSURL  *url = [NSURL URLWithString:stringURL];
 NSData *urlData = [NSData dataWithContentsOfURL:url];
if ( urlData )
{
 NSArray*paths=NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
  NSString  *documentsDirectory = [paths objectAtIndex:0];  

 NSString  *filePath = [NSString stringWithFormat:@"%@/%@", documentsDirectory,@"filename.png"];
 [urlData writeToFile:filePath atomically:YES];
} 

You can store in Document directory,Below is example to store a image .Below is link explained to store audio video file

How to download audio/video files from internet and store in iPhone app?

Community
  • 1
  • 1
Mukesh
  • 3,680
  • 1
  • 15
  • 32
0

Document Directory is best to store data.

NSString *path = [[self applicationDocumentsDirectory].path 
                       stringByAppendingPathComponent:@"fileName.txt"];
[sampleText writeToFile:path atomically:YES
                       encoding:NSUTF8StringEncoding error:nil];

- (NSURL *)applicationDocumentsDirectory {
    return [[[NSFileManager defaultManager] URLsForDirectory:NSDocumentDirectory 
         inDomains:NSUserDomainMask] lastObject];
}
Tejas Ardeshna
  • 4,343
  • 2
  • 20
  • 39
  • I'm not sure about this... It depends on what data to store. Apple rejected me Apps several times for storing images (probably too much) in Doc folder. – Bisca May 15 '15 at 07:26
0

It depends on what will you need to do with data. If data can be recreated or downloaded, save it in temp o cacje directory. If data is created by user and have to be backed up, you can use Documents to sync with iCloud. I recommend you to read Apple documentation because if you this wrong way, your App could be rejected. https://developer.apple.com/library/ios/documentation/FileManagement/Conceptual/FileSystemProgrammingGuide/FileSystemOverview/FileSystemOverview.html

You can exclude your files to be backed up (and stored in Documents folder) https://developer.apple.com/library/ios/qa/qa1719/_index.html

Bisca
  • 6,380
  • 2
  • 19
  • 32