How to display NSData with PDF content in iOS UIWebView?

I am trying to show PDF in my iPad application via UIWebView as shown below.

 NSData * responseData = [NSURLConnection sendSynchronousRequest:mutableUrlRequest returningResponse:nil error:nil]; [pdfWebView loadData:responseData MIMEType:@"application/pdf" textEncodingName:@"utf-8" baseURL:nil]; 

But that does not work. Is this the correct way to display? If only someone could show me the PDF in UIWebView or any other useful method.

Thanks!

+7
source share
6 answers

Got an answer to my question. The bottom line will do the magic.

  [webview loadData:PdfContent MIMEType:@"application/pdf" textEncodingName:@"utf-8" baseURL:nil]; 
+21
source

Save it to a file with the suffix PDF in the temp directory and open it from there.

+2
source

How to save NSData (pDF file) to the document directory:

 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]; 
+2
source

SWIFT3 :

 webView.load(data, mimeType: "application/pdf", textEncodingName: "UTF-8", baseURL: URL(string: "You can use URL of any file in your bundle here")!) 
+2
source

if you have a PDF file bundled with your application (in this example, named "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]; 

More information can be found here: Technical QA1630: Using UIWebView to display selected document types.

0
source

For any reason, are you not using either the Quick Look Framework or the UIDocumentmentInteractionController? They can provide the user with more functionality than WebView, and a little less cloogy.

Here are links to Apple documentation: https://developer.apple.com/library/ios/documentation/FileManagement/Conceptual/DocumentInteraction_TopicsForIOS/Articles/UsingtheQuickLookFramework.html

https://developer.apple.com/library/ios/documentation/uikit/reference/UIDocumentInteractionController_class/Reference/Reference.html

If you want to render a PDF file from the application file system, no need to worry! Just make sure the saved pdf file has the correct extension ".pdf" and then provide it with the QLPreviewController in the full path:

 self.contentURL = [NSURL fileURLWithPath:fullPath]; 
0
source

All Articles