【问题标题】:downloading a pdf into app将pdf下载到应用程序
【发布时间】:2011-05-30 09:43:24
【问题描述】:

我遇到了一个问题,在我的应用程序中,我正在创建从 url 下载 pdf 文件,它必须保存在我的应用程序中。谁能给我建议如何下载文件并将其存储在应用程序中.

谢谢大家。

【问题讨论】:

标签: iphone objective-c ipad sqlite


【解决方案1】:

使用NSData 从 Internet 检索文件(同步):

NSURL *downloadUrl = [NSURL URLWithString:@"mydomain.tld/myfile.pdf"];
NSData *data = [NSData dataWithContentsOfURL:downloadUrl];
[data writeToURL:filePath atomically:YES];

要在不阻塞主线程(异步)的情况下下载数据,请查看此帖子的答案:

iPhone SDK: How do you download video files to the Document Directory and then play them?

您不能将任何文件保存到您的应用程序包中,但您可以将它们保存到特定于应用程序的 Documents 目录中:

NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0]; 
NSString *path = [documentsDirectory stringByAppendingPathComponent:@"myfile.pdf"]; 

NSURL *filePath = [NSURL fileURLWithPath:path];

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2021-01-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多