我想用Swift在WebView中显示一个网站。 这是我的代码:
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)
}
}
在var req = NSURLRequest(URL:url)
我收到错误,
"可选类型的价值' NSURL?'没有打开;你的意思是使用"!" ...."
如果我加了!并运行它,我得到一个运行时错误,
"此类不是密钥浏览器的密钥值编码兼容"
答案 0 :(得分:1)
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.
}
}