我想用 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)
我收到错误,
"Value of optional type 'NSURL?' not unwrapped; did you mean to use"!"...."
如果我添加 !并运行它,我收到运行时错误,
"this class is not key value coding-compliant for the key browser"
最佳答案
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.
}
}
关于ios - Swift 的 NSURLRequest 问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27094513/