Reputation: 695
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")
}
}
Upvotes: 1
Views: 708
Reputation: 695
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")
}
Upvotes: 1