简体   繁体   中英

Enable Pinch to Zoom gesture when open PDF file in WKWebView on iOS 10

I'm looking for a way to enable the "pinch to zoom" magnification gesture when open the local PDF file in WKWebview on iOS 10. As I know the pinch to zoom is enabled on the iOS 12

class ViewController: UIViewController {
   var wkWebView: WKWebView?
   @IBOutlet var webView: UIView!
   fileprivate var delegate = AuthenticatedWebViewNavigationDelegate()

   override func viewDidLoad() {
     super.viewDidLoad()

     if wkWebView == nil {
        createWebView()
     }
     let filePath = Bundle.main.path(forResource: "local", ofType: 
      "pdf")
     let baseUrl = URL(fileURLWithPath: filePath!)
     wkWebView?.loadFileURL(baseUrl, allowingReadAccessTo: baseUrl)

   }
}

class AuthenticatedWebViewNavigationDelegate: NSObject, WKNavigationDelegate {
    weak var viewController: ViewController?

    public func webView(_ webView: WKWebView, decidePolicyFor navigationAction: WKNavigationAction, decisionHandler: @escaping (WKNavigationActionPolicy) -> Void) {
            decisionHandler(.allow)
       }

    public func webView(_ webView: WKWebView, decidePolicyFor navigationResponse: WKNavigationResponse, decisionHandler: @escaping (WKNavigationResponsePolicy) -> Void) {
            decisionHandler(.allow)
       }

    public func webView(_ webView: WKWebView, didFailProvisionalNavigation navigation: WKNavigation!, withError error: Error) { 
            print("error")
       }

    public func webView(_ webView: WKWebView, didFinish navigation: WKNavigation!) {
            print("finish")
       }
    public func webView(_ webView: WKWebView, didFail navigation: WKNavigation!, withError error: Error) {
           print("error")
       }
}

I found the scrollview.pinchGestureRecognizer is disabled on the wkwebview of iOS 10

So the solution is to enable the pinchGestureRecognizer, I put the function in the following function:

public func webView(_ webView: WKWebView, didFinish navigation: WKNavigation!) {
    if #available(iOS 11.0, *) {
    } else {
      if webView.url?.scheme == "file" {                       
         webView.scrollView.pinchGestureRecognizer?.isEnabled = true
       }
    }

        print("finish")
   }

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