下载 PDF 文件并将其保存到临时目录 iOS

Downloading a PDF file and saving it to temporary directory iOS

我的应用程序显示列表中的 PDF 文件。我正在努力理解 iOS 中的文件管理。我在这里阅读了指南,但没有帮助:https://developer.apple.com/library/mac//documentation/FileManagement/Conceptual/FileSystemProgrammingGuide/Introduction/Introduction.html#//apple_ref/doc/uid/TP40010672

我不知道如何访问 /tmp 文件夹来写入。我想我需要使用 NSURLConnectionURLsForDirectory:inDomains: 方法。但我不知道 URLsForDirectory:inDomains: 方法对 return 临时目录采用什么参数以及如何将 NSData*NSURLConnection 转换为 PDF 文件。

您使用以下代码获取存储在 /tmp 中的文件的文件路径:

NSURL *tmpDirURL = [NSURL fileURLWithPath:NSTemporaryDirectory() isDirectory:YES];        // get /tmp folder path

NSURL *fileURL = [[tmpDirURL URLByAppendingPathComponent:@"filename"] URLByAppendingPathExtension:@"jpg"];

NSLog(@"fileURL: %@", [fileURL path]);

相反,您可以将文件保存到 /Documents 文件夹,稍后在不需要时将其删除。

看看:

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

1) 使用 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];

2) 使用 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];

参考 : nsurlsession-tutorial link.

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