ios - wkwebview ios 10在重新创建webview后停止使用localfileurl加载文件
<p><p>我对 wkwebview 和 ios 10.3 和 swift 3 有一个奇怪的行为。</p>
<p>我正在使用 wkwebview 来显示本地 html 文件(在应用程序支持下)。
首次启动时一切正常,本地网站(包括 js 和 css)运行良好。但是,如果我从 Internet 获取内容更新(这涉及删除 html 目录和提取新文件),webview 将不会加载文件。如果我只是尝试重用现有的 webview:</p>
<pre><code>self.webView?.loadFileURL(URL(fileURLWithPath: ( sharedConfig.WEBROOT + self.getIndexPage(requestProps.landingPage))), allowingReadAccessTo: sharedConfig.BASEURL)
</code></pre>
<p>甚至使用 javascript 调用 <code>window.location.href='\(self.landingPage)'</code> 我在调试控制台中收到相当奇怪的错误消息:</p>
<blockquote>
<p>Surface creation failed for size: (0 0) and format: (0)</p>
</blockquote>
<p>如果我尝试使用以下命令重新初始化 wkwebview:</p>
<p>` ...</p>
<pre><code> 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()
_ = self.webView?.loadFileURL(localFileUrl, allowingReadAccessTo: sharedConfig.BASEURL)
}
</code></pre>
<p>什么都没有发生 - 显示的 html 没有变化,没有错误,没有卡住 - 本地网站照常工作(涉及 loadFileURL 调用的函数除外 - 但此代码在 ios 9.3 中有效,我确信它在 ios 10 中有效.</p>
<p>我还知道 ios 10.3 beta 中存在 wkwebview 问题和带有空格的文件路径的错误 - 但我也使用 Documents 目录对此进行了测试,结果相同。</p>
<p>但过了一会儿我得到了这个日志输出:</p>
<blockquote>
<p>Could not signal service com.apple.WebKit.Networking: 113: Could not find specified service</p>
</blockquote>
<p>这个答案<a href="https://stackoverflow.com/questions/44585980/com-apple-webkit-webcontent-drops-113-error-could-not-find-specified-service" rel="noreferrer noopener nofollow">here</a>表示,它可能与未将 webview 正确添加为 subview 有关。但是我看不到我在将 webview 添加到 subview 时是否做错了...</p>
<p><strong>更新</strong></p>
<p> View 层次结构的进一步调试结果是“wkwebview 的位置不明确”。</p>
<p><code>po [ _autolayoutTrace]</code> 结果如下:</p>
<pre><code>•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
</code></pre>
<p>但现在我还有更多问题:</p>
<ul>
<li>这种奇怪的 WKWebView 行为可能是“不明确的布局”造成的吗?</li>
<li>如果是这样,我该如何解决这种歧义? </li>
</ul>
<p><strong>更新 2:</strong></p>
<p>感谢 <a href="https://stackoverflow.com/questions/18756640/width-and-height-equal-to-its-superview-using-autolayout-programmatically" rel="noreferrer noopener nofollow">this answer</a>我可以解决模棱两可的布局,并且 View 层次结构调试器很高兴 - 但不幸的是,这并没有解决 webview 的问题 - 它仍然没有响应 loadFileURL 请求!</p></p>
<br><hr><h1><strong>Best Answer-推荐答案</ strong></h1><br>
<p><p>好的,我终于找到了解决办法:</p>
<p>我不小心创建了一个新的 ViewController 对象,而不是使用当前实例,并且我的代码在新的 ViewController 上初始化了另一个 webview。 </p>
<p>所以我以某种方式丢失了对旧的可见 webview 的引用,因此它不再响应。 </p>
<p>使用正确的实例后一切正常。但是我仍然想知道为什么这没有导致指向此的其他一些错误。 </p></p>
<p style="font-size: 20px;">关于ios - wkwebview ios 10在重新创建webview后停止使用localfileurl加载文件,我们在Stack Overflow上找到一个类似的问题:
<a href="https://stackoverflow.com/questions/44769752/" rel="noreferrer noopener nofollow" style="color: red;">
https://stackoverflow.com/questions/44769752/
</a>
</p>
页:
[1]