使用WkWebView预览本地pdf文件

1 项目环境描述 pdf.pdf文件在工程中,使用WkWebView进行预览

 if let pdfPath = Bundle.main.path(forResource: "pdf", ofType: "pdf") {
            // 使用 WKWebView 查看pdf
            let pdfReader = ETPDFReaderViewController("WKWebView 查看 pdf", pdfPath)
            self.present(pdfReader, animated: true, completion: nil)
        }

2 封装的ETPDFReaderViewController的代码如下:
导入:
import UIKit
import WebKit

class ETPDFReaderViewController: UIViewController {

    var pdfPath: String?
    var sharedClosure: (() -> ())?
    
    fileprivate var webView = WKWebView()
    fileprivate var loadingView = UIActivityIndicatorView()
    fileprivate let statusHeight = UIApplication.shared.statusBarFrame.height
    
    private let kStatusHeight = UIApplication.shared.statusBarFrame.height
    
    fileprivate lazy var sharedBarButton: UIBarButtonItem = { [weak self] in
        let shared = UIBarButtonItem.init(title: "分享", style: .plain, target: self, action: #selector(sharedAction))
        return shared
    }()
    
    
    class func `init`(_ title: String, _ pdfPath: String) -> ETPDFReaderViewController {
        let pdfReaderVC = ETPDFReaderViewController()
        pdfReaderVC.title = title
        pdfReaderVC.pdfPath = pdfPath
        
        return pdfReaderVC
    }
    
    override func viewDidLoad() {
        super.viewDidLoad()

        // Do any additional setup after loading the view.
        var webViewFrame = UIScreen.main.bounds
        if navigationController != nil {
            navigationItem.rightBarButtonItem = self.sharedBarButton
        } else {
            webViewFrame.origin.y = (kStatusHeight + CGFloat.init(44))
            webViewFrame.size.height -= (kStatusHeight + CGFloat.init(44) + CGFloat.init((kStatusHeight > 20 ? 34 : 0)))
            // 自定义导航栏
            /// 1.导航栏背景
            let navigationBackgroundView = UIView.init(frame: CGRect.init(x: 0, y: 0, width: UIScreen.main.bounds.width, height: webViewFrame.origin.y))
            navigationBackgroundView.backgroundColor = UIColor.groupTableViewBackground // 背景颜色
            
            self.view.addSubview(navigationBackgroundView)
            
            /// 2.返回按钮
            let backBtn = UIButton.init(type: .custom)
            backBtn.frame = CGRect.init(x: 10, y: kStatusHeight, width: 50, height: 44)
            backBtn.setTitle("返回", for: .normal)
            backBtn.setTitleColor(UIColor.blue, for: .normal)
            backBtn.addTarget(self, action: #selector(backAction), for: .touchUpInside)
            navigationBackgroundView.addSubview(backBtn)
            
            /// 3.标题
            let titleLabel = UILabel.init(frame: CGRect.init(x: (navigationBackgroundView.frame.size.width - 180)*0.5, y: kStatusHeight, width: 180, height: 44))
            titleLabel.font = UIFont.systemFont(ofSize: 18)
            titleLabel.textAlignment = .center
            titleLabel.text = self.title
            navigationBackgroundView.addSubview(titleLabel)
            
            /// 4.分享按钮
            let sharedBtn = UIButton.init(type: .custom)
            sharedBtn.frame = CGRect.init(x: navigationBackgroundView.frame.size.width-50-10, y: kStatusHeight, width: 50, height: 44)
            sharedBtn.setTitle("分享", for: .normal)
            sharedBtn.setTitleColor(UIColor.blue, for: .normal)
            sharedBtn.addTarget(self, action: #selector(sharedAction), for: .touchUpInside)
            navigationBackgroundView.addSubview(sharedBtn)
            
            /// 5.分割线
            let lineView = UIView.init(frame: CGRect.init(x: 0, y: navigationBackgroundView.frame.height-1, width: navigationBackgroundView.frame.width, height: 1))
            lineView.backgroundColor = UIColor.gray
            navigationBackgroundView.addSubview(lineView)
        }
        
        self.webView.backgroundColor = UIColor.white
        self.webView.frame = webViewFrame
        self.webView.navigationDelegate = self
        self.view.addSubview(self.webView)
        
        self.loadingView.frame = self.view.bounds
        self.loadingView.hidesWhenStopped = true
        self.loadingView.activityIndicatorViewStyle = .gray
        self.loadingView.startAnimating()
        self.view.addSubview(self.loadingView)
        
        
        self.loadPDF()
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }
    
    private func loadPDF() {
        if let path = self.pdfPath {
            let url = URL.init(fileURLWithPath: path)
            let urlRequest = URLRequest.init(url: url, cachePolicy: .reloadIgnoringCacheData, timeoutInterval: 45)
            self.webView.load(urlRequest)
        }
    }
    
    @objc func sharedAction() {
        debugPrint("sharedAction")
        self.sharedClosure?()
    }
    
    @objc func backAction() {
        self.dismiss(animated: true, completion: nil)
    }

}

extension ETPDFReaderViewController: WKNavigationDelegate {
    func webView(_ webView: WKWebView, didFinish navigation: WKNavigation!) {
        self.loadingView.stopAnimating()
    }
}

©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • 发现 关注 消息 iOS 第三方库、插件、知名博客总结 作者大灰狼的小绵羊哥哥关注 2017.06.26 09:4...
    肇东周阅读 12,303评论 4 61
  • 开始,我以为他是个憨傻;现在,我怀疑自己错了。 他的名字有些怪,三个字当中,一个是数量字,另二个是描述体积的字。直...
    自由心空阅读 428评论 6 9
  • 一天了,小蜗牛们此时肯定已安然入睡,特别想知道任务完成的怎么样? 这个周末安排了宝爸宝妈们带小蜗...
    话三石阅读 386评论 0 7
  • 我依靠在一棵大树下等雨停,却不知不觉睡着了。不知道从什么时候起有只小鸟停留在树枝上,睡梦中仿佛有人在敲着我的脑袋叫...
    秋水明阅读 268评论 0 1