• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    迪恩网络公众号

ios - wkwebview ios 10在重新创建webview后停止使用localfileurl加载文件

[复制链接]
菜鸟教程小白 发表于 2022-12-11 19:58:02 | 显示全部楼层 |阅读模式 打印 上一主题 下一主题

我对 wkwebview 和 ios 10.3 和 swift 3 有一个奇怪的行为。

我正在使用 wkwebview 来显示本地 html 文件(在应用程序支持下)。 首次启动时一切正常,本地网站(包括 js 和 css)运行良好。但是,如果我从 Internet 获取内容更新(这涉及删除 html 目录和提取新文件),webview 将不会加载文件。如果我只是尝试重用现有的 webview:

self.webView?.loadFileURL(URL(fileURLWithPath: ( sharedConfig.WEBROOT + self.getIndexPage(requestProps.landingPage))), allowingReadAccessTo: sharedConfig.BASEURL)

甚至使用 javascript 调用 window.location.href='\(self.landingPage)' 我在调试控制台中收到相当奇怪的错误消息:

Surface creation failed for size: (0 0) and format: (0)

如果我尝试使用以下命令重新初始化 wkwebview:

` ...

    DispatchQueue.main.async {

    for subview in self.view.subviews {
        if subview is WKWebView {
            subview.removeFromSuperview()
        }
    }

    let contentController = WKUserContentController();
    contentController.add(
        self,
        name: "callbackHandler"
    )
    let processPool: WKProcessPool = WKProcessPool.init()

    let config = WKWebViewConfiguration()
    config.processPool = processPool
    config.userContentController = contentController
     ...

    self.webView = WKWebView(
        frame: CGRect.zero,
        configuration: config
    )

    self.webView!.uiDelegate = self
    self.webView!.navigationDelegate = self

    self.webView!.translatesAutoresizingMaskIntoConstraints = false
    self.webView!.allowsBackForwardNavigationGestures = true

    let localfilePath = sharedConfig.WEBROOT + absPath

    let localFileUrl = URL(fileURLWithPath: localfilePath)


    self.webView?.tag = 12
    self.view.addSubview(self.webView!)
    let height = NSLayoutConstraint(item: self.webView!, attribute: .height, relatedBy: .equal, toItem: self.view, attribute: .height, multiplier: 1, constant: 0)
    let width = NSLayoutConstraint(item: self.webView!, attribute: .width, relatedBy: .equal, toItem: self.view, attribute: .width, multiplier: 1, constant: 0)
    self.view.addConstraints([height, width])
    _ = self.webView?.loadFileURL(localFileUrl, allowingReadAccessTo: sharedConfig.BASEURL)
    }

什么都没有发生 - 显示的 html 没有变化,没有错误,没有卡住 - 本地网站照常工作(涉及 loadFileURL 调用的函数除外 - 但此代码在 ios 9.3 中有效,我确信它在 ios 10 中有效.

我还知道 ios 10.3 beta 中存在 wkwebview 问题和带有空格的文件路径的错误 - 但我也使用 Documents 目录对此进行了测试,结果相同。

但过了一会儿我得到了这个日志输出:

Could not signal service com.apple.WebKit.Networking: 113: Could not find specified service

这个答案here表示,它可能与未将 webview 正确添加为 subview 有关。但是我看不到我在将 webview 添加到 subview 时是否做错了...

更新

View 层次结构的进一步调试结果是“wkwebview 的位置不明确”。

po [[UIWindow keyWindow] _autolayoutTrace] 结果如下:

•UIWindow:0x7fbce4b06380 - AMBIGUOUS LAYOUT
|   UITransitionView:0x7fbce2424cc0
|   |   •UIView:0x7fbce2611430
|   |   |   *_UILayoutGuide:0x7fbce2612150
|   |   |   *_UILayoutGuide:0x7fbce260cf10
|   |   |   *WKWebView:0x7fbce508b000'Appname'- AMBIGUOUS LAYOUT for WKWebView:0x7fbce508b000'Appname'.minX{id: 72}, WKWebView:0x7fbce508b000'Appname'.minY{id: 73}
|   |   |   |   WKScrollView:0x7fbce508b600
|   |   |   |   |   WKContentView:0x7fbce5086000
|   |   |   |   |   |   UIView:0x7fbce25194c0
|   |   |   |   |   |   |   UIView:0x7fbce25136e0
|   |   |   |   |   |   |   |   WKCompositingView:0x7fbce261f4f0
|   |   |   |   |   |   |   |   |   WKCompositingView:0x7fbce26201c0
|   |   |   |   |   |   |   |   |   |   WKCompositingView:0x7fbce2620020
|   |   |   |   |   |   |   |   |   |   |   WKCompositingView:0x7fbce261fce0
|   |   |   |   |   |   |   |   |   |   |   |   WKCompositingView:0x7fbce2620500
|   |   |   |   |   |   |   |   |   |   |   WKCompositingView:0x7fbce261fe80
|   |   |   |   |   |   |   |   |   |   |   |   WKCompositingView:0x7fbce251a4d0
|   |   |   |   |   |   |   |   |   |   |   |   |   WKCompositingView:0x7fbce251a330
|   |   |   |   |   |   |   |   |   |   |   |   |   |   WKCompositingView:0x7fbce2522790
|   |   |   |   |   |   |   |   |   |   |   |   |   |   |   UIScrollView:0x7fbce4069000
|   |   |   |   |   |   |   |   |   |   |   |   |   |   |   |   WKCompositingView:0x7fbce251e230
|   |   |   |   |   |   |   |   |   |   WKCompositingView:0x7fbce261e280
|   |   |   |   |   |   |   |   |   WKCompositingView:0x7fbce2620360
|   |   |   |   |   UIView:0x7fbce2509060
|   |   |   |   |   UIImageView:0x7fbce4b1fb00
|   |   |   |   |   UIImageView:0x7fbce4b1f910

Legend:
    * - is laid out with auto layout
    + - is laid out manually, but is represented in the layout engine because translatesAutoresizingMaskIntoConstraints = YES
    • - layout engine host

但现在我还有更多问题:

  • 这种奇怪的 WKWebView 行为可能是“不明确的布局”造成的吗?
  • 如果是这样,我该如何解决这种歧义?

更新 2:

感谢 this answer我可以解决模棱两可的布局,并且 View 层次结构调试器很高兴 - 但不幸的是,这并没有解决 webview 的问题 - 它仍然没有响应 loadFileURL 请求!



Best Answer-推荐答案


好的,我终于找到了解决办法:

我不小心创建了一个新的 ViewController 对象,而不是使用当前实例,并且我的代码在新的 ViewController 上初始化了另一个 webview。

所以我以某种方式丢失了对旧的可见 webview 的引用,因此它不再响应。

使用正确的实例后一切正常。但是我仍然想知道为什么这没有导致指向此的其他一些错误。

关于ios - wkwebview ios 10在重新创建webview后停止使用localfileurl加载文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44769752/

回复

使用道具 举报

懒得打字嘛,点击右侧快捷回复 【右侧内容,后台自定义】
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

关注0

粉丝2

帖子830918

发布主题
阅读排行 更多
广告位

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap