0

I am trying to open external links in WKWebView and for this I found a solution. and implemented delegates. but I am getting Thread 1: signal SIGABRT error

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)
    }
}

but when i remove these delegate methods it does not crashes

i have tried applying debug pints inside all of the methods but app crashes at startup and it moves to debug navigator ......

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    
Asad kamran
  • 440
  • 10
  • 21
  • Can you point out the line at which you are getting the error – Sandeep Bhandari Dec 20 '17 at 07:36
  • 1
    ok debuging.... – Asad kamran Dec 20 '17 at 07:38
  • edited the question you can have a look at it – Asad kamran Dec 20 '17 at 07:42
  • You sure your web view isn't nil correct ?? all the answers posted in the question you posted has web view being instantiated and added as subview to View. In your case it seems to be IBOutlet. So make sure it isnt nil – Sandeep Bhandari Dec 20 '17 at 07:45
  • 1
    yes as i remove these methods app runs fine but as i implement them it moves to thread navigator – Asad kamran Dec 20 '17 at 07:47
  • Do you have a crash log? Also, did you put breakpoints to each delegate method to check wich one causes the crash? – Larme Dec 20 '17 at 09:37
  • I removed all of the methods and just let the method with `didStartProvisionalNavigation ` and my application crashed then I removed all unnecessary methods other than `func webView(_ webView: WKWebView,createWebViewWith configuration: WKWebViewConfiguration, for navigationAction: WKNavigationAction, windowFeatures: WKWindowFeatures) -> WKWebView?` my app didint crash this time. And then i checked the soruce of delegates and didnt found these extra methods up there in both `,WKNavigationDelegate,WKUIDelegate` – Asad kamran Dec 20 '17 at 11:20
  • I tested your code, didn't get an issue on iOS11. I get an issue on iOS10, don't know why yet if I set the Interface Builder compatibility to iOS10, I can't build: "WKWebView before iOS 11.0 (NSCoding support was broken in previous versions)" See there https://stackoverflow.com/questions/46221577/xcode-9-gm-wkwebview-nscoding-support-was-broken-in-previous-versions – Larme Dec 20 '17 at 12:27

0 Answers0