【问题标题】:Objective-c: Downloading a PDF fileObjective-c:下载 PDF 文件
【发布时间】:2012-02-24 00:11:03
【问题描述】:

我正在尝试启用一个按钮以从服务器下载 pdf 文件。 (进入设备) 我没有成功使用此代码(发布此问题,我已更改地址) 你能给我一些建议让它工作吗..? 提前致谢。

NSData *pdfData = [[NSData alloc] initWithContentsOfURL:[NSURL     URLWithString:@"http://www.mydomain.com/mypdffile.pdf"]];

//Store the Data locally as PDF File
NSString *resourceDocPath = [[NSString alloc] initWithString:[[[[NSBundle mainBundle]  resourcePath] stringByDeletingLastPathComponent] stringByAppendingPathComponent:@"<Application_Home>/Documents/"]];

NSString *filePath = [resourceDocPath stringByAppendingPathComponent:@"myPDF.pdf"];
[pdfData writeToFile:filePath atomically:YES];

//Now create Request for the file that was saved in your documents folder
NSURL *url = [NSURL fileURLWithPath:filePath];
NSURLRequest *requestObj = [NSURLRequest requestWithURL:url];
[webView setUserInteractionEnabled:YES];
[webView setDelegate:self];
[webView loadRequest:requestObj];

【问题讨论】:

    标签: iphone ios pdf download


    【解决方案1】:

    您没有构建正确的 NSURL 对象来访问服务器。 fileURLWithPath: 是一种构建指向文件系统中文件的 URL 的方法。

    使用 URLWithString:编写完整的 url,例如“http://myserver.com/myfile.pdf”

    【讨论】:

    • 您好,感谢您的指出。我使用了 URLWithString,但什么也没发生。它说[切换到进程 7171 线程 0x1c03]
    • 您确定网址正确吗?您是否对字符串中的任何特殊字符使用了 url 编码版本?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2016-08-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多