How to Disable " Pinch Gesture" Effect with Swift 4 in Xcode9

Asked

Viewed 78 times

0

How do I disable my Wkwebview zoom? My app is allowing zoom in 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) {}

1 answer

0

first make this other Extension of your class that implements the Uiscrollviewdelegate:


 extension ViewController: UIScrollViewDelegate {

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

then, inside your viewDidLoad place that the delegate of the scrollView of your webView is your class:


 webView.scrollView.delegate = self

so it will cancel the zoom done with the gesture of "tweezers"

  • So I did this but if I click on some field to fill in, then it expands...

  • If I click on a field, it activates as a touch highlight (which is triggered on iOS by the style rule).

Browser other questions tagged

You are not signed in. Login or sign up in order to post.