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

时间:2015-06-12 09:09:50

标签: ios objective-c pdf nsurlconnection nsfilemanager

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

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

4 个答案:

答案 0 :(得分:3)

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链接。

答案 1 :(得分:1)

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

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

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

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

答案 2 :(得分:0)

相反,您可以将文件保存到/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;
}

答案 3 :(得分:0)

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