What you really need is to monitor the user’s navigation. You can do this in Uiwebview as well but Uiwebview ta deprecated so I recommend using Wkwebview for this:
import UIKit
import WebKit
class ViewController: UIViewController, WKNavigationDelegate {
let webView = WKWebView()
override func viewDidLoad() {
super.viewDidLoad()
print("Documents\n", FileManager.documents)
let url = URL(string: "https://www.google.com")!
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!
}
And implement the method func webView(_ webView: WKWebView, decidePolicyFor navigationAction: WKNavigationAction, decisionHandler: @escaping (WKNavigationActionPolicy) -> Void)
to detect when the user clicks a link and if this link is a pdf:
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)
}
}
}
Certo, @Leo esse método func webView(_ webView: WKWebView, decidePolicyFor navigationAction: WKNavigationAction, decisionHandler: aescaping (WKNavigationActionPolicy) -> Void). That’s what’s going on at delegate.Swift ?
– R.Park
You can put it in any file within your project. You can also put it in Viewcontroler underneath everything. You can also take it out of the extension and put it right into your view controller
– Leo Dabus
is presenting this error: "Wkwebview before iOS 11.0 (Nscoding support was Broken in Previous versions) " . Meaning I am working with version 10 and it seems that it does not support version less than 11.
– R.Park
I’m sure this error is not from my iOS10 code supports Wkwebview for sure. I’m not even using Nscoding. I can’t solve your problem without seeing your code. What you need your Viewcontroller inherit from Nscoding for?
– Leo Dabus
I also found it strange, I’ll take a look at my "Main.storyboard" the problem is there. But as for your code, it’s all right, I just imported Webkit, it’s reset, no mistakes.
– R.Park
Solved dude! Thanks. I just don’t know why he’s zooming in on the app.
– R.Park
You’re welcome, good luck :)
– Leo Dabus
You can use a customUserAgent in your Wkwebview using the Safari pro mac user agent and make it display the desktop version of the site
– Leo Dabus
I get it, I’m going to look around here. Once again, thank you ! :)
– R.Park
It is difficult to take this effect "Pinch zoom". I tried the following..: func scrollViewWillBeginZooming(_ scrollView: Uiscrollview, with view: Uiwebviewnavigationtype?) { scrollView.pinchGestureRecognizer?. isEnabled = false } Did not roll... Have any idea how I can do this scheme that you said?
– R.Park
https://stackoverflow.com/a/31943976/2303865
– Leo Dabus
That’s the first one I tried, but it didn’t work.
– R.Park