我正在使用swift 3,我正试图以编程方式加载一个webview,像这样:
let webview = UIWebView()
webview.scalesPageToFit = true
webview.autoresizesSubviews = true
webview.autoresizingMask = [.flexibleWidth, .flexibleHeight]
webview.frame = CGRect(x: 0, y: 0, width: 800, height: 800)
webview.delegate = self
let localUrl = String(format:"http://www.google.com")
let url = NSURL.fileURL(withPath: localUrl)
webview.loadRequest(URLRequest(url: url))
self.view.addSubview(webview)
但是我得到的只是一个空白的屏幕。我没有得到任何错误,请帮助!
et webview = UIWebView()
webview.scalesPageToFit = true
webview.autoresizesSubviews = true
webview.autoresizingMask = [.flexibleWidth, .flexibleHeight]
webview.frame = CGRect(x: 0, y: 0, width: 800, height: 800)
webview.delegate = self
webview.loadRequest(URLRequest(url: URL(string: "https://www.google.com")!))
self.view.addSubview(webview)
这对我来说是有效的,虽然它不适合,在iPhone 7模拟器上,除非你将web视图框架更改为webview.frame = self.view.frame
。你试图将字符串转换为文件url,然后加载文件url而不是正常url。此外,最近,苹果使http://whatever.com不能工作,你需要使用https://whatever.com.
一个问题是http://www.google.com
不是文件URL。所以你不能从中得到fileURL
。只需要一个普通的URL。
另一个问题,在iOS 9和10,是你不能,默认情况下,加载http
URL。你必须使用https