简体   繁体   中英

How to display NSData with PDF content in iOS UIWebView?

I am trying to show a PDF in my iPad application through 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 this doesn't work. Is it a right way to display? If not could anyone guide me in showing the PDF in UIWebView or any other useful method.

Thanks !

Got the answer for my question. The below line will do the magic.

 [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后缀的文件,然后从那里打开它。

How to save NSData( pDF file) into 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];

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];

You can find more information here: Technical QA1630: Using UIWebView to display select document types.

Any reason you aren't using either the Quick Look Framework or UIDocumentInteractionController? They're able to provide the user with more functionality than a WebView and a little less cloogy.

Here are links to Apple's 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're looking to render the PDF from the app's filesystem, no need to fret! Just make sure that the pdf you have stored has a proper '.pdf' extension, then provide it to the QLPreviewController in full path:

self.contentURL = [NSURL fileURLWithPath:fullPath];

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM