Shantanu Paul Shantanu Paul - 6 days ago 6
Objective-C Question

Downloading a PDF file and saving it to temporary directory iOS

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.

Answer

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