Cleptric Cleptric - 26 days ago 8
iOS Question

NSURLRequest issue with Swift

I want to display a website in a WebView with Swift.
This is my code:

import UIKit
import WebKit


class ViewController: UIViewController {


@IBOutlet var containerView : UIView! = nil
var webView: WKWebView?

override func loadView() {
super.loadView()

self.webView = WKWebView()
self.view = self.webView!
}

override func viewDidLoad() {
super.viewDidLoad()

var url = NSURL(string:"http://www.google.de/")
var req = NSURLRequest(URL:url)
self.webView!.loadRequest(req)
}

}


at
var req = NSURLRequest(URL:url)
I get an error,


"Value of optional type 'NSURL?' not unwrapped; did you mean to use"!"...."


If I add the ! and run it, I got an runtime error,


"this class is not key value coding-compliant for the key browser"

Answer Source
import UIKit

class ViewController: UIViewController {

    @IBOutlet weak var wv: UIWebView!


    func loadUrl(){
        wv.loadRequest(NSURLRequest(URL: NSURL(string: "http://www.google.de/")!))
    }
    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view, typically from a nib.
        loadUrl()

    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }


}