ios - Swift 的 NSURLRequest 问题

标签 ios swift webkit

我想用 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/

相关文章:

css - 无法摆脱蓝色突出显示并且无法检测到它所在的元素

iphone - 使用 Phonegap 安装 iTunes 同步应用程序失败

ios - MFMailComposeViewController : toRecepients Methods Not Working Properly

ios - Dealloc 被调用但实时内存没有减少

ios - 按对象数组过滤 searchController

html - CSS3 webkit 过渡

ios - 在箭头形面具周围绘制边框

ios - 如何将 NSURLRequest 的结果传回包含类

具有多个 imageView 输出的 iOS 相机

javascript - 在 webkit 中渲染后,如何获取 <body> 元素的大小?