[英]Open NSData (PDF content) in UIWebview which is stored in Sqlite
[英]How to display NSData with PDF content in iOS UIWebView?
我試圖通過UIWebView
在我的iPad應用程序中顯示PDF,如下所示。
NSData * responseData = [NSURLConnection sendSynchronousRequest:mutableUrlRequest returningResponse:nil error:nil];
[pdfWebView loadData:responseData MIMEType:@"application/pdf" textEncodingName:@"utf-8" baseURL:nil];
但這不起作用。 這是一種正確的展示方式嗎? 如果沒有,任何人都可以指導我在UIWebView中顯示PDF或任何其他有用的方法。
謝謝 !
得到了我的問題的答案。 以下行將發揮魔力。
[webview loadData:PdfContent MIMEType:@"application/pdf" textEncodingName:@"utf-8" baseURL:nil];
SWIFT3 :
webView.load(data, mimeType: "application/pdf", textEncodingName: "UTF-8", baseURL: URL(string: "You can use URL of any file in your bundle here")!)
將其保存到臨時目錄中帶有PDF后綴的文件,然后從那里打開它。
如何將NSData(pDF文件)保存到文檔目錄中:
NSArray *searchPaths = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES);
NSString *documentFolderPath = [searchPaths objectAtIndex: 0];
NSString *imagePath = [documentFolderPath stringByAppendingPathComponent:@"yourPdfFileName.pdf"];
NSFileManager *filemanager=[NSFileManager defaultManager];
if(![filemanager fileExistsAtPath:filePath])
[[NSFileManager defaultManager] createFileAtPath:filePath contents: nil attributes: nil];
NSFileHandle *handle = [NSFileHandle fileHandleForWritingAtPath:filePath];
[handle writeData:data];
如果您的應用程序捆綁了PDF文件(在此示例中名為“document.pdf”):
UIWebView *webView = [[UIWebView alloc] initWithFrame:CGRectMake(10, 10, 200, 200)];
NSString *path = [[NSBundle mainBundle] pathForResource:@"document" ofType:@"pdf"];
NSURL *targetURL = [NSURL fileURLWithPath:path];
NSURLRequest *request = [NSURLRequest requestWithURL:targetURL];
[webView loadRequest:request];
[self.view addSubview:webView];
[webView release];
您可以在此處找到更多信息:技術QA1630:使用UIWebView
顯示選擇的文檔類型。
您沒有使用Quick Look Framework或UIDocumentInteractionController的任何原因? 他們能夠為用戶提供比WebView更多的功能和更少的cloogy。
以下是Apple文檔的鏈接: https : //developer.apple.com/library/ios/documentation/FileManagement/Conceptual/DocumentInteraction_TopicsForIOS/Articles/UsingtheQuickLookFramework.html
如果您希望從應用程序的文件系統中呈現PDF,則無需擔心! 只需確保您存儲的pdf具有正確的'.pdf'擴展名,然后以完整路徑將其提供給QLPreviewController:
self.contentURL = [NSURL fileURLWithPath:fullPath];
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.