oo-di-lolly
oo-di-lolly

Reputation: 213

Having trouble with WKWebview and UIWebView in iOS 9

I'm getting really frustrated with this, hope anyone who experienced with similar issues can help. Current project using Swift 2, iOS9 and XCode 7.2. I have ViewConroller and trying to add a UIWebView or a WKWebview as a subview, however, UIWebView crashes the app, while my WKWebview is not loading URL, and none of the WKNavigationDelegate methods get called.

  1. First try use to use UIWebView, the app crash on the line that UIWebView init with frame, error 'EXC_BAD_ACCESS'. If I add the webview in the nib, the whole app crash when trying to present this viewcrontroller with the same error.

``

import UIKit
import WebKit

class TestViewController: UIViewController, UIWebViewDelegate {
    @IBOutlet weak var placeholderView: UIView!
    override func viewDidLoad() {
    super.viewDidLoad()
    let webV:UIWebView = UIWebView(frame: CGRectMake(0, 0, placeholderView.bounds.width, placeholderView.bounds.height))
    webV.loadRequest(NSURLRequest(URL: NSURL(string: "https://www.google.com")))
    webV.delegate = self;
    self.view.addSubview(webV)
    }
}

``

  1. Use WKWebview instead, this solved the crash issue, however, it only display a blank view, and none of the WKNavigationDelegate methods get called

``

import UIKit
import WebKit

class TestViewController: UIViewController, WKNavigationDelegate {
    @IBOutlet weak var placeholderView: UIView!
    override func viewDidLoad() {
        super.viewDidLoad()
        let preferences = WKPreferences()
        preferences.javaScriptEnabled = false
        let webcon = WKWebViewConfiguration()
        webcon.preferences = preferences
        let webView: WKWebView = WKWebView(frame: CGRectMake(0, 0, placeholderView.bounds.width, placeholderView.bounds.height), configuration: webcon)
        webView.navigationDelegate = self
        let url = NSURL(string: "https://www.google.com")!
        placeholderView.addSubview(webView)
        placeholderView.bringSubviewToFront(webView)
        webView.loadRequest(NSURLRequest(URL: url))

    }
    func webView(webView: WKWebView, didStartProvisionalNavigation navigation: WKNavigation!) {
        NSLog("Webview started Loading")
    }
    func webView(webView: WKWebView, decidePolicyForNavigationAction navigationAction: WKNavigationAction, decisionHandler: (WKNavigationActionPolicy) -> Void) {
        NSLog("%@", navigationAction.request.URL!)
    }

    func webView(webView: WKWebView, didFinishNavigation navigation: WKNavigation!) {
        print("\nWebview did finish load \(webView.request)", terminator: "")
    }
}

``

I already have imported WebKit into frame work, and added app allows arbitrary loads into my info.plist.

<key>NSAppTransportSecurity</key>
    <dict>
        <key>NSAllowsArbitraryLoads</key>
        <true/>
    </dict>

Upvotes: 0

Views: 4851

Answers (2)

Jason Honcheung Wong
Jason Honcheung Wong

Reputation: 65

Hmm, just try to change your webView declaration.. declare your webView first in your class by var webView: WKWebView (don't use let), and then put self.webView = WKWebView(frame: CGRectMake(0, 0, placeholderView.bounds.width, placeholderView.bounds.height), configuration: webcon) in viewDidLoad()

Upvotes: 1

Aaron Brager
Aaron Brager

Reputation: 66224

The reason UIWebView is crashing is almost certainly because delegate is set to an object that doesn't exist.

I'm guessing the WKWebView delegate methods aren't called because you haven't set the navigationDelegate property (or you did, but the object you set it to was deallocated.)

Upvotes: 1

Related Questions