Objective-c:下载 PDF 文件

发布于 2025-01-01 11:29:37 字数 909 浏览 0 评论 0原文

我正在尝试启用一个按钮来从服务器下载 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];

I am trying to eneble a button to download a pdf file from a server.
(Onto the divice)
I'm not being successful with this code (to post this question, i've changed the address)
Would you give me some advice to make it work..?
Thanks in advance.

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

德意的啸 2025-01-08 11:29:37

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

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

You are not building a correct NSURL object to reach a server. fileURLWithPath: is a method to build URL that points to files in the file system.

Use URLWithString: writing the complete url, that is for example "http://myserver.com/myfile.pdf"

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文