【问题标题】:Open some items in UIWebView, and others in external browser在 UIWebView 中打开一些项目,在外部浏览器中打开其他项目
【发布时间】:2016-01-08 16:17:54
【问题描述】:

我正在创建一个嵌入 UIWebView 的 html5 应用程序。我已将 webview 委托给控制器,并且在 shouldStartLoadWithRequest 上,我正在寻找要在外部浏览器上打开或不打开的 url 模式。

效果很好!

但是当手机没有连接时,我正在加载本地 html 文件。因为 shouldStartLoadWithRequest 在第一次请求时返回 FALSE,offline.html 没有加载

有人做过吗?

【问题讨论】:

  • 请将代码作为文本添加到您的问题中,而不是作为屏幕截图。
  • 抱歉,我的 xcode 在 VMbox 中!

标签: ios swift uiwebview


【解决方案1】:

设置路径后立即尝试:

do {
    let path = NSBundle.mainBundle().pathForResource("offline", ofType: "html", inDirectory:"offline")
    let string = try String(contentsOfFile: path!, encoding:NSUTF8StringEncoding)
    webView.loadHTMLString(string, baseURL: NSURL(string: "http://"))
} catch {
    print(error)
}

let url = NSURL(fileURLWithPath:NSBundle.mainBundle().pathForResource("offline", ofType:"html", inDirectory: "offline")!)
let request = NSURLRequest(URL: url);
webView.loadRequest(request);

我试过了,效果很好。

确保您的“离线”目录是文件夹(蓝色)而不是组(黄色)。

希望对您有所帮助。

【讨论】:

  • 第一个解决方案,是一个很好的解决方案,但不起作用。 offline.html 做白屏
  • 我刚才试了一下,效果很好。重试我的新更改。
  • 我发现了我的问题。我没有触发错误-1003(未找到主机)!移除开关工作完美
【解决方案2】:

我发现了问题。我没有触发错误 -1003(未找到主机)!移除开关完美!

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2022-10-24
    • 1970-01-01
    • 2022-12-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多