我正在尝试在WKWebView
中打开外部链接,为此我找到了solution。并实施了代表。但我得到线程1:信号SIGABRT错误
import UIKit
import WebKit
class ViewController: UIViewController,WKNavigationDelegate,WKUIDelegate {
@IBOutlet var webView: WKWebView!
override func viewDidLoad() {
super.viewDidLoad()
webView.navigationDelegate = self
webView.uiDelegate = self
// Do any additional setup after loading the view, typically from a nib.
}
override func viewDidAppear(_ animated: Bool) {
super.viewDidAppear(animated)
let mUrl = URL(string: "https://www.motorscity.com/mobile/en")
let mRequest = URLRequest(url: mUrl!)
webView.load(mRequest)
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
func webView(_ webView: WKWebView, didStartProvisionalNavigation navigation: WKNavigation) {
UIApplication.shared.isNetworkActivityIndicatorVisible = true
print("didStartProvisionalNavigation: \(navigation)")
}
func webView(_ webView: WKWebView, didReceiveServerRedirectForProvisionalNavigation navigation: WKNavigation) {
print("didReceiveServerRedirectForProvisionalNavigation: \(navigation)")
}
func webView(_ webView: WKWebView, didFailProvisionalNavigation navigation: WKNavigation, withError error: Error) {
print("didFailProvisionalNavigation: \(navigation), error: \(error)")
}
func webView(_ webView: WKWebView, didFinishLoading navigation: WKNavigation) {
UIApplication.shared.isNetworkActivityIndicatorVisible = false
print("didFinishLoadingNavigation: \(navigation)")
}
func _webViewWebProcessDidCrash(_ webView: WKWebView) {
print("WebContent process crashed; reloading")
}
func webView(_ webView: WKWebView,createWebViewWith configuration: WKWebViewConfiguration, for navigationAction: WKNavigationAction, windowFeatures: WKWindowFeatures) -> WKWebView?{
//if <a> tag does not contain attribute or has _blank then navigationAction.targetFrame will return nil
if let trgFrm = navigationAction.targetFrame {
if(!trgFrm.isMainFrame){
UIApplication.shared.isNetworkActivityIndicatorVisible = true
self.webView.load(navigationAction.request)
}
}
return nil
}
func webView(_ webView: WKWebView, didFinish navigation: WKNavigation!){
UIApplication.shared.isNetworkActivityIndicatorVisible = false
print("didFinish: \(String(describing: self.webView.url)); stillLoading:\(self.webView.isLoading ? "NO" : "YES")")
}
func webView(_ webView: WKWebView, decidePolicyFor navigationResponse: WKNavigationResponse, decisionHandler: @escaping (_: WKNavigationResponsePolicy) -> Void) {
print("decidePolicyForNavigationResponse")
decisionHandler(.allow)
}
func webView(_ webView: WKWebView, decidePolicyFor navigationAction: WKNavigationAction, decisionHandler: @escaping (_: WKNavigationActionPolicy) -> Void) {
decisionHandler(.allow)
}
}
但是当我删除这些委托方法时,它不会崩溃
我尝试在所有方法中应用调试pints但是应用程序在启动时崩溃并且它移动到调试导航器......
dyld`__abort_with_payload:
0x1189be20c <+0>: movl $0x2000209, %eax ; imm = 0x2000209
0x1189be211 <+5>: movq %rcx, %r10
0x1189be214 <+8>: syscall
-> 0x1189be216 <+10>: jae 0x1189be220 ; <+20>
0x1189be218 <+12>: movq %rax, %rdi
0x1189be21b <+15>: jmp 0x1189bda74 ; cerror_nocancel
0x1189be220 <+20>: retq
0x1189be221 <+21>: nop
0x1189be222 <+22>: nop
0x1189be223 <+23>: nop