无法使用 WKWebView 在 safari 中打开外部 link
Unable to open external link in safari using WKWebView
我正在从 UIWebView
迁移到 WKWebView
。需要在 WKWebView
中加载 HTML 内容一切正常,但问题是在单击 [=17 中的任何 link 后无法在外部 safari 浏览器中打开 link =].我使用了下面的代码
Class
class CTFSPolicyAndTermsViewController: UIViewController, WKUIDelegate, WKNavigationDelegate {}
出口和WKWebView
变量
@IBOutlet weak var webViewContainer: UIView!
var webView: WKWebView!
viewDidLoad
方法
override func viewDidLoad() {
super.viewDidLoad()
webView = WKWebView()
self.webView.uiDelegate = self
self.webView.navigationDelegate = self
let webConfiguration = WKWebViewConfiguration()
webView = WKWebView(frame: .zero, configuration: webConfiguration)
webView.translatesAutoresizingMaskIntoConstraints = false
webViewContainer.addSubview(webView)
[webView.topAnchor.constraint(equalTo: webViewContainer.topAnchor),
webView.bottomAnchor.constraint(equalTo: webViewContainer.bottomAnchor),
webView.leftAnchor.constraint(equalTo: webViewContainer.leftAnchor),
webView.rightAnchor.constraint(equalTo: webViewContainer.rightAnchor)].forEach {
anchor in
anchor.isActive = true
}
用于加载 HTML 内容
if(self.isTermsMode)
{
let path:String = Bundle.main.path(forResource: "abc", ofType: "html")!
let text = try? String(contentsOfFile: path, encoding: String.Encoding.utf8)
webView!.loadHTMLString(text!, baseURL: nil)
}
决定政策
func webView(_ webView: WKWebView, decidePolicyFor navigationAction: WKNavigationAction, decisionHandler: @escaping (WKNavigationActionPolicy) -> Void) {
if let url = navigationAction.request.url {
print(url)
UIApplication.shared.open(url)
decisionHandler(.cancel)
} else {
decisionHandler(.allow)
}
}
decidePolicyFor
方法应该调用但它没有执行我找不到问题
我错过了订单
let webConfiguration = WKWebViewConfiguration()
webView = WKWebView(frame: .zero, configuration: webConfiguration)
webView.translatesAutoresizingMaskIntoConstraints = false
webViewContainer.addSubview(webView)
[webView.topAnchor.constraint(equalTo: webViewContainer.topAnchor),
webView.bottomAnchor.constraint(equalTo: webViewContainer.bottomAnchor),
webView.leftAnchor.constraint(equalTo: webViewContainer.leftAnchor),
webView.rightAnchor.constraint(equalTo:
webViewContainer.rightAnchor)].forEach { anchor in
anchor.isActive = true
}
self.webView.uiDelegate = self
self.webView.navigationDelegate = self
我正在从 UIWebView
迁移到 WKWebView
。需要在 WKWebView
中加载 HTML 内容一切正常,但问题是在单击 [=17 中的任何 link 后无法在外部 safari 浏览器中打开 link =].我使用了下面的代码
Class
class CTFSPolicyAndTermsViewController: UIViewController, WKUIDelegate, WKNavigationDelegate {}
出口和WKWebView
变量
@IBOutlet weak var webViewContainer: UIView!
var webView: WKWebView!
viewDidLoad
方法
override func viewDidLoad() {
super.viewDidLoad()
webView = WKWebView()
self.webView.uiDelegate = self
self.webView.navigationDelegate = self
let webConfiguration = WKWebViewConfiguration()
webView = WKWebView(frame: .zero, configuration: webConfiguration)
webView.translatesAutoresizingMaskIntoConstraints = false
webViewContainer.addSubview(webView)
[webView.topAnchor.constraint(equalTo: webViewContainer.topAnchor),
webView.bottomAnchor.constraint(equalTo: webViewContainer.bottomAnchor),
webView.leftAnchor.constraint(equalTo: webViewContainer.leftAnchor),
webView.rightAnchor.constraint(equalTo: webViewContainer.rightAnchor)].forEach {
anchor in
anchor.isActive = true
}
用于加载 HTML 内容
if(self.isTermsMode)
{
let path:String = Bundle.main.path(forResource: "abc", ofType: "html")!
let text = try? String(contentsOfFile: path, encoding: String.Encoding.utf8)
webView!.loadHTMLString(text!, baseURL: nil)
}
决定政策
func webView(_ webView: WKWebView, decidePolicyFor navigationAction: WKNavigationAction, decisionHandler: @escaping (WKNavigationActionPolicy) -> Void) {
if let url = navigationAction.request.url {
print(url)
UIApplication.shared.open(url)
decisionHandler(.cancel)
} else {
decisionHandler(.allow)
}
}
decidePolicyFor
方法应该调用但它没有执行我找不到问题
我错过了订单
let webConfiguration = WKWebViewConfiguration()
webView = WKWebView(frame: .zero, configuration: webConfiguration)
webView.translatesAutoresizingMaskIntoConstraints = false
webViewContainer.addSubview(webView)
[webView.topAnchor.constraint(equalTo: webViewContainer.topAnchor),
webView.bottomAnchor.constraint(equalTo: webViewContainer.bottomAnchor),
webView.leftAnchor.constraint(equalTo: webViewContainer.leftAnchor),
webView.rightAnchor.constraint(equalTo:
webViewContainer.rightAnchor)].forEach { anchor in
anchor.isActive = true
}
self.webView.uiDelegate = self
self.webView.navigationDelegate = self