2012-02-01 76 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