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?
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?
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?
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];
}
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