iOS:子类化UIWebView URLRequest崩溃

时间:2017-05-22 20:25:44

标签: ios swift3 uiwebview xcode8

我是UIWebView的子类,但当我尝试发出请求时,它会因此错误而崩溃:

fatal error: unexpectedly found nil while unwrapping an Optional value

这是我的实施:

的ViewController:

var web:MyWebView!

    override func viewDidLoad() {
        super.viewDidLoad()
self.webPage = MyWebView(frame: self.view.frame)
        self.view.bringSubview(toFront: self.webPage!)
}

class MyWebView: UIWebView,UIWebViewDelegate {


let urlStr = "http://apple.com"

override init(frame: CGRect) {
    super.init(frame: frame)
    scalesPageToFit = true
    scrollView.bounces = false
    delegate = self
    let urlRequest = URL(string: urlStr)
    let request = URLRequest(url: urlRequest!)
    self.loadRequest(request)

}

如果在控制台中检查urlRequest的值:

po urlRequest
nil

你们中的任何人都知道我做错了什么,或者为什么这个urlRequest设置为nil?

我非常感谢你的帮助。

1 个答案:

答案 0 :(得分:0)

这对我很有用

import UIKit

class ViewController: UIViewController {

    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view, typically from a nib.
    }

    override func viewDidAppear(_ animated: Bool) {

        let webView = MyWebView.init(frame: self.view.frame)
        self.view.addSubview(webView)

    }

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

}

然后从viewcontroller中调用它

mysqli_query($db, "INSERT into doctors(name, spec, qualification, image) VALUES('$name', '$spec', '$qua', '$image'));

enter image description here