【问题标题】:Swift - webview wont loadSwift - webview 不会加载
【发布时间】:2016-11-10 01:27:49
【问题描述】:

我正在使用 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)

但我得到的只是一个空白的白色屏幕。我完全没有错误,请帮助!

【问题讨论】:

    标签: ios swift webview swift3


    【解决方案1】:
    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一个普通的网址。另外,最近苹果做了,所以http://whatever.com不起作用,你需要使用https://whatever.com

    【讨论】:

    • "您试图将字符串转换为文件 url,然后加载文件 url 而不是普通 url。另外,最近,Apple 做到了,所以 whatever.com 不起作用,你需要使用whatever.com。”与我已经说过的非常相似。
    • 对不起,我以为添加代码 sn -p 会更清楚一点。
    • 同意,当然。
    • 不好意思抄袭,下次如果有和别人相似的地方,我会指出来的。
    • 听起来不错。例如,您可以通过参考我的回答来开始您的回答。您有权通过显示代码来改进另一个答案。
    【解决方案2】:

    一个问题是http://www.google.com 不是文件URL。所以你不能用它制作fileURL。只需创建一个纯 URL。

    另一个问题是,在 iOS 9 和 10 上,默认情况下您无法加载 http URL。你必须使用https

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2015-06-28
      • 2018-10-18
      • 2014-09-16
      • 1970-01-01
      • 1970-01-01
      • 2012-10-06
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多