我使用的Xcode 6.1.1店從內部網絡PDF格式,並顯示到的WebView
開發一個應用程序適用於iOS 8.1和我想下載從互聯網上一個PDF,店到我的iPad,然後加載它成網頁視圖;這是PDF測試:
這是結果我得到在iPad上嘗試顯示它在我的WebView後:
這是到目前爲止我的代碼...
// Get the PDF Data from the url in a NSData Object
NSData *pdfData = [[NSData alloc] initWithContentsOfURL:[NSURL URLWithString:@"http://www.ferc.gov/docs-filing/elibrary/larg-file.pdf"]];
// Store the Data locally as PDF File
NSArray *searchPaths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentFolderPath = searchPaths[0];
NSString *archivePath = [documentFolderPath stringByAppendingPathComponent:@"larg-file.pdf"];
[pdfData writeToFile:archivePath atomically:YES];
// Now create Request for the file that was saved in your documents folder
NSURL *url = [NSURL fileURLWithPath:archivePath];
NSURLRequest *requestObj = [NSURLRequest requestWithURL:url];
[_webView setUserInteractionEnabled:YES];
[_webView setDelegate:self];
[_webView loadRequest:requestObj];
我該如何做這項工作?
編輯:這就是答案
NSArray *searchPaths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentFolderPath = searchPaths[0];
NSString *archivePath = [documentFolderPath stringByAppendingPathComponent:@"larg-file.pdf"];
if (![Functions isAlreadyStoredinDeviceFileWithName:@"larg-file.pdf"]) {
NSData *pdfData = [[NSData alloc] initWithContentsOfURL:[NSURL URLWithString:@"http://www.ferc.gov/docs-filing/elibrary/larg-file.pdf"]];
[pdfData writeToFile:archivePath atomically:YES];
}
// Now create Request for the file that was saved in your documents folder
NSURL *url = [NSURL fileURLWithPath:archivePath];
NSURLRequest *requestObj = [NSURLRequest requestWithURL:url];
[_webView setUserInteractionEnabled:YES];
[_webView setDelegate:self];
[_webView loadRequest:requestObj];
你如何在Webview中加載? –
爲什麼使用網絡視圖?爲什麼不使用'QLPreviewController'? – rmaddy
僅供參考 - 您構建「文檔」路徑的邏輯錯誤,並且在iOS 8下無法運行。 – rmaddy