我正在从迁移UIWebView
到WKWebView
. 需要在WKWebView
所有内容中加载 HTML 内容工作正常,但问题是在单击任何链接后无法在外部 safari 浏览器中打开链接WKWebView
。我用下面的代码
班级
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
方法应该调用但它没有执行我无法找到问题