2012-02-01 78 views
1

我试图创建一个按钮来从服务器下载pdf文件。 (在设备上) 我没有成功使用此代码(发布这个问题,我已经改变了地址) 你能给我一些建议,使其工作..? 在此先感谢。Objective-c:正在下载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]; 

回答

1

您不构建正确的NSURL对象以到达服务器。 fileURLWithPath:是一种构建指向文件系统中文件的URL的方法。

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

+0

嗨,谢谢指出。我用过URLWithString,但没有发生任何事情。它说[切换到进程7171线程0x1c03] – user973067 2012-02-01 10:28:57

+0

你确定这个网址是否正确?你有没有使用url编码版本的字符串中的任何特殊字符? – Gabriel 2012-02-01 10:47:09

+0

是的,它显示在webView中的文件,所以URL是正确的。 – user973067 2012-02-01 11:32:46