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

查看:39
本文介绍了如何在 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 建议您不要将 .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
            }

        }
    }

}

从 Asset.xcassets (Swift 4) 访问 PDF

if let asset = NSDataAsset(name: "myPDF") {
            let url = Bundle.main.bundleURL
            let webView = WKWebView(frame: CGRect(x:20,y:20,width:view.frame.size.width-40, height:view.frame.size.height-40))
            webView.load(asset.data, mimeType: "application/pdf", characterEncodingName:"", baseURL:url)
            view.addSubview(webView)
       }

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

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