Gerry Gerry - 4 months ago 14
iOS Question

The webView is nil in iOS 8 but it's not nil in iOS 9 using xib

Why the webView is nil in iOS 8 but not in iOS 9?



and how should I use webview in iOS 8?
Here is the code (swift 2.2)

in a
viewController
class:

@IBOutlet weak var webView: UIWebView!

override func viewDidLoad() {
super.viewDidLoad()

self.webView?.delegate = self // without '?' in iOS 8 crash
// in iOS 9 not crash

// debug code
if let webView = self.webView { // iOS 8 nil
print("\(webView)") // iOS 9 not nil
}

//...
}


Thanks!

Answer
class ViewController: UIViewController, UIWebViewDelegate {
@IBOutlet weak var webView: UIWebView!

let url = NSURL(string: "http://google.com")


override func viewDidLoad() {
    super.viewDidLoad()

    self.webView.delegate = self

    let req = NSURLRequest(URL : url!)
    self.webView.loadRequest(req)
}

func webViewDidStartLoad(webView: UIWebView) {
    print("start loading")
}

func webViewDidFinishLoad(webView: UIWebView) {
    print("finish loading")
}

//...

}

like this, this code can run perfectly in iOS8 & iOS9

Comments