当用户单击webView中的链接或URL时,我正在尝试启动Safari浏览器。
我有一个带有webView的viewcontroller,可加载请求。在webView上,有一个链接,如果单击该链接,Safari将启动。我已经将webView设置为委托。但这似乎不起作用。它总是将链接加载到嵌入式视图中。我在这里找到的一些答案是旧的目标C代码,它们不起作用。我希望有人能帮忙。
谢谢。
import SafariServices
import UIKit
import WebKit
class AboutViewController: UIViewController, UIWebViewDelegate {
@IBOutlet weak var spinner: UIActivityIndicatorView!
@IBOutlet weak var webView: WKWebView!
override func viewDidLoad() {
super.viewDidLoad()
// Start the spinner
spinner.alpha = 1
spinner.startAnimating()
//Set delegate for the webview
webView.navigationDelegate = self
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
override func viewWillAppear(_ animated: Bool) {
let path = Bundle.main.path(forResource: "about", ofType: "html")
guard path != nil else { //Guard statement is good to check whether something is true. So in this case, it reads "confirm if path is not nil else do something else"
print("Error: Can't find the file")
return
}
//Create a url object from the string path
let url = URL(fileURLWithPath: path!)
//Create a request
let request = URLRequest(url: url)
//Load the request
webView.load(request)
}
func webView(_: WKWebView, shouldStartLoadWith: URLRequest, navigationType: WKNavigationType ) -> Bool {
if navigationType == WKNavigationType.linkActivated {
UIApplication.shared.open(shouldStartLoadWith.url!, options: [:], completionHandler: nil)
return false
}
return true
}
}
答案 0 :(得分:1)
我正在使用此线程(WKWebView open links from certain domain in safari)使其正常工作。
这是答案:
import SafariServices
import UIKit
import WebKit
class AboutViewController: UIViewController, WKNavigationDelegate {
@IBOutlet weak var spinner: UIActivityIndicatorView!
@IBOutlet weak var webView: WKWebView!
override func viewDidLoad() {
super.viewDidLoad()
// Start the spinner
spinner.alpha = 1
spinner.startAnimating()
let path = Bundle.main.path(forResource: "about", ofType: "html")
guard path != nil else { //Guard statement is good to check whether something is true. So in this case, it reads "confirm if path is not nil else do something else"
print("Error: Can't find the file")
return
}
//Create a url object from that string path
let url = URL(fileURLWithPath: path!)
//Set delegate for the webview
webView.navigationDelegate = self
//Load the request
webView.load(URLRequest(url: url))
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
override func viewWillAppear(_ animated: Bool) {
}
func webView(_ webView: WKWebView, decidePolicyFor navigationAction: WKNavigationAction, decisionHandler: @escaping (WKNavigationActionPolicy) -> Void) {
if navigationAction.navigationType == .linkActivated {
if let url = navigationAction.request.url,
let host = url.host, !host.hasPrefix("www.google.com"),
UIApplication.shared.canOpenURL(url) {
UIApplication.shared.open(url)
//print(url)
// print("Redirected to browser. No need to open it locally")
decisionHandler(.cancel)
} else {
// print("Open it locally")
decisionHandler(.allow)
}
} else {
//print("not a user click")
decisionHandler(.allow)
}
}
func webView(_ webView: WKWebView, didFinish navigation: WKNavigation!) {
spinner.stopAnimating()
spinner.alpha = 0
}
}