2
votes

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 NSURLConnectionto PDF file.

4
The link you provided has same content as mine.Shantanu Paul

4 Answers

3
votes

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.

1
votes

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]);
0
votes

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;
}
0
votes
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];