如何在Swift中的UIWebView中加载本地PDF [英] How to Load Local PDF in UIWebView in Swift

查看:111
本文介绍了如何在Swift中的UIWebView中加载本地PDF的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以我目前正在尝试显示我在UIWebview中的本地PDF,这是我正在使用的代码:

So I am currently trying to display a local PDF I have in UIWebview and this is the code I'm using:

@IBOutlet weak var webView:UIWebView!
override func viewDidLoad() {
    super.viewDidLoad()    

var pdfLoc = NSURL(fileURLWithPath:NSBundle.mainBundle().pathForResource("Sample", ofType:"pdf")!) 
var request = NSURLRequest(URL: pdfLoc);
self.webView.loadRequest(request);
}

代码将成功构建,但是当我运行应用程序时,它会崩溃错误:
线程1:EXC_BAD_INSTRUCTION(代码= EXC-I386_INVOP,子代码= 0x0)

The code will successfully build, but when I run the app, it will crash with the error: Thread 1: EXC_BAD_INSTRUCTION(code=EXC-I386_INVOP,subcode=0x0)

我找到了一些有关如何执行此操作的教程,但它们都已经过时或者在Objective-C中。

I have found a few tutorials on how to do this, but they are all very outdated or in Objective-C.

推荐答案

在这里:

if let pdf = NSBundle.mainBundle().URLForResource("myPDF", withExtension: "pdf", subdirectory: nil, localization: nil)  {
            let req = NSURLRequest(URL: pdf)
            let webView = UIWebView(frame: CGRectMake(20,20,self.view.frame.size.width-40,self.view.frame.size.height-40))
            webView.loadRequest(req)
            self.view.addSubview(webView)
        }

修改

替代方案是通过NSData:

The alternative is via NSData:

if let pdfURL = NSBundle.mainBundle().URLForResource("myPDF", withExtension: "pdf", subdirectory: nil, localization: nil),data = NSData(contentsOfURL: pdfURL), baseURL = pdfURL.URLByDeletingLastPathComponent  {
    let webView = UIWebView(frame: CGRectMake(20,20,self.view.frame.size.width-40,self.view.frame.size.height-40))
    webView.loadData(data, MIMEType: "application/pdf", textEncodingName:"", baseURL: baseURL)
    self.view.addSubview(webView)
}

Apple make a建议你不要将.loadRequest用于本地HTML文件,而不是明确地将其扩展到其他数据类型。所以我在上面提供了NSData路由。如果你想指定textEncodingName,它可以是utf-8,utf-16等。

Apple make a point of advising you to not use .loadRequest for local HTML files, while not clearly extending this to other data types. So I've provided the NSData route above. If you wish to specify a textEncodingName it can be "utf-8", "utf-16", etc.

编辑:Swift 3

这是一个Swift 3版本的代码,正如Apple建议的那样,使用WKWebView代替UIWebView。

Here's a Swift 3 version of the code using, as Apple advise, WKWebView in place of UIWebView.

import UIKit
import WebKit

class ViewController: UIViewController {
    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view, typically from a nib.

        if let pdfURL = Bundle.main.url(forResource: "myPDF", withExtension: "pdf", subdirectory: nil, localization: nil)  {
            do {
                let data = try Data(contentsOf: pdfURL)
                let webView = WKWebView(frame: CGRect(x:20,y:20,width:view.frame.size.width-40, height:view.frame.size.height-40))
                webView.load(data, mimeType: "application/pdf", characterEncodingName:"", baseURL: pdfURL.deletingLastPathComponent())
               view.addSubview(webView)

            }
            catch {
                // catch errors here
            }

        }
    }

}

这篇关于如何在Swift中的UIWebView中加载本地PDF的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆