[英]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.