How to disable pinch gesture effect with Swift 4 in Xcode9

0

How do I disable the zoom of my WKWebView? My application is allowing zooming on the screen, I would like to disable this function. How should I proceed?

import UIKit
import WebKit

class ViewController: UIViewController, WKNavigationDelegate {
  @IBOutlet weak
  var myWebView: UIWebView!
    var webView = WKWebView()

  override func viewDidLoad() {
    super.viewDidLoad()

    print("Documents\n", FileManager.documents)
    let url = URL(string: "http://site/") !
      webView.frame = view.bounds
    webView.navigationDelegate = self
    webView.load(URLRequest(url: url))
    webView.autoresizingMask = [.flexibleWidth, .flexibleHeight]
    view.addSubview(webView)
  }
}


extension FileManager {
  static
  let documents = FileManager.default.urls(
    for: .documentDirectory, in: .userDomainMask).first!
}

extension ViewController {
  public func webView(_ webView: WKWebView, decidePolicyFor navigationAction: WKNavigationAction, decisionHandler: @escaping(WKNavigationActionPolicy) - > Void) {
    if navigationAction.navigationType == .linkActivated {
      if let url = navigationAction.request.url,
        url.pathExtension == "pdf" {
          print("pdf url:\n", url, "\nNo need to open locally")
          decisionHandler(.cancel)

          // se voce quiser pode download o pdf
          URLSession.shared.downloadTask(with: url) {
            location,
            response,
            error in
            guard
            let location = location,
              let response = response
            else {
              print("error:", error ? ? "nil")
              return
            }
            print("Location:", location.path, "\nResponse:", response, "\nFilename:", response.suggestedFilename ? ? "file.pdf")
            do {
              let destination = FileManager.documents.appendingPathComponent(response.suggestedFilename ? ? "file.pdf")
              print("destination:", destination.path)
              try FileManager.default.moveItem(at: location, to: destination)
              print("file moved from temporary location to documents directory")
            } catch {
              print("File copy/move error:", error)
            }
          }.resume()
          // ou exibir usando o safari
          if UIApplication.shared.canOpenURL(url) {
            UIApplication.shared.open(url)
            print(url)
            print("abrindo pdf com default browser")
          }
        } else {
          print("user clicked on a link that it is not a pdf")
          decisionHandler(.allow)
        }
    } else {
      decisionHandler(.allow)
    }
  }
}

func webView(_ webView: WKWebView, decidePolicyFor navigationAction: WKNavigationAction, decisionHandler: @escaping(WKNavigationActionPolicy) - > Void) {}
    
asked by anonymous 03.08.2018 / 20:44

1 answer

0

first make this another extension of your class that implements UIScrollViewDelegate:


 extension ViewController: UIScrollViewDelegate {

   func scrollViewWillBeginZooming(_ scrollView: UIScrollView, with view: UIView?) {
        scrollView.pinchGestureRecognizer?.isEnabled = false          
   }
 }
 

Then, within your viewDidLoad, place the delegate of the scrollView of your webView into your class:


 webView.scrollView.delegate = self

so it will cancel zooming done with the "clamp" gesture

    
10.08.2018 / 21:22