Reputation: 705
My app displays PDF files from a list. I am struggling to understand the File Management in iOS. I read the guide here but it isn't helpful: https://developer.apple.com/library/mac//documentation/FileManagement/Conceptual/FileSystemProgrammingGuide/Introduction/Introduction.html#//apple_ref/doc/uid/TP40010672
I don't get how to access the /tmp
folder to write to it. I guess I'll need to use NSURLConnection
with URLsForDirectory:inDomains:
method. But I do not know what parameter the URLsForDirectory:inDomains:
method takes to return the temporary directory and how to convert NSData*
from NSURLConnection
to PDF file.
Upvotes: 2
Views: 6783
Reputation: 49
NSData *dataPdf = [NSData dataWithContentsOfURL:pdfOnline.url];
//Get path directory
NSArray *paths =
NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,
NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
//Create PDF_Documents directory
documentsDirectory = [documentsDirectory
stringByAppendingPathComponent:@"PDF_Documents"];
[[NSFileManager defaultManager]
createDirectoryAtPath:documentsDirectory withIntermediateDirectories:YES attributes:nil error:nil];
NSString *filePath = [NSString stringWithFormat:@"%@/%@",documentsDirectory, @"**PUT FILENAME YOU WANT**"];
[dataPdf writeToFile:filePath atomically:YES];
Upvotes: 0
Reputation: 38239
1) Use NSURLConnection :
NSURL *fileURL = [NSURL URLWithString:@"your url here"];
NSURLSession *session = [NSURLSession sharedSession];
[[session dataTaskWithURL:fileURL completionHandler:^(NSData *data,
NSURLResponse *response,
NSError *error)
{
if(!error)
{
NSString *filePath = [NSTemporaryDirectory() stringByAppendingPathComponent:[response suggestedFilename]];
[data writeToFile:filePath atomically:YES];
}
}] resume];
OR
2) Use NSURLSession :
NSURL *fileURL = [NSURL URLWithString:@"url here"];
NSURLSession *session = [NSURLSession sharedSession];
[[session dataTaskWithURL:fileURL completionHandler:^(NSData *data,
NSURLResponse *response,
NSError *error)
{
if(!error)
{
NSString *filePath = [NSTemporaryDirectory() stringByAppendingPathComponent:[response suggestedFilename]];
[data writeToFile:filePath atomically:YES];
}
}] resume];
Refer : nsurlsession-tutorial link.
Upvotes: 3
Reputation: 1547
Instead you can save the file to /Documents
folder and delete it later when it's not needed.
Have a look:
+(NSString *)writeDataToDocuments:(NSData *)data withFilename:(NSString *)filename{
NSString *docsPath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) firstObject];
NSString *filePath = [[NSString alloc] initWithString: [docsPath stringByAppendingPathComponent:filename]];
[data writeToFile:filePath atomically:YES];
return filePath;
}
Upvotes: 0
Reputation: 2285
You get file path of file stored in /tmp
using below code:
NSURL *tmpDirURL = [NSURL fileURLWithPath:NSTemporaryDirectory() isDirectory:YES]; // get /tmp folder path
NSURL *fileURL = [[tmpDirURL URLByAppendingPathComponent:@"filename"] URLByAppendingPathExtension:@"jpg"];
NSLog(@"fileURL: %@", [fileURL path]);
Upvotes: 1