重新创建webview后,wkwebview ios 10停止加载带有localfileurl的文件
我对wkwebview、ios 10.3和swift 3有一种奇怪的行为 我正在使用wkwebview显示本地html文件(在应用程序支持下)。 刚启动时,一切正常,本地网站(包括js和css)正常运行。但是,如果我从internet获取内容更新(包括删除html目录和提取新文件),webview将不会加载这些文件。如果我只是尝试使用以下工具重用现有webview:重新创建webview后,wkwebview ios 10停止加载带有localfileurl的文件,ios,swift3,autolayout,wkwebview,uiview-hierarchy,Ios,Swift3,Autolayout,Wkwebview,Uiview Hierarchy,我对wkwebview、ios 10.3和swift 3有一种奇怪的行为 我正在使用wkwebview显示本地html文件(在应用程序支持下)。 刚启动时,一切正常,本地网站(包括js和css)正常运行。但是,如果我从internet获取内容更新(包括删除html目录和提取新文件),webview将不会加载这些文件。如果我只是尝试使用以下工具重用现有webview: self.webView?.loadFileURL(URL(fileURLWithPath: ( sharedConfig.WEB
self.webView?.loadFileURL(URL(fileURLWithPath: ( sharedConfig.WEBROOT + self.getIndexPage(requestProps.landingPage))), allowingReadAccessTo: sharedConfig.BASEURL)
甚至使用javascript调用window.location.href='\(self.landingPage)
我在调试控制台中得到了一条非常奇怪的错误消息:
大小:(0)和格式:(0)的曲面创建失败
如果我尝试使用以下方法重新初始化wkwebview:
self.webView?.loadFileURL(URL(fileURLWithPath: ( sharedConfig.WEBROOT + self.getIndexPage(requestProps.landingPage))), allowingReadAccessTo: sharedConfig.BASEURL)
`
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版中有一个bug,其中有wkwebview问题和带有空格的文件路径,但我也用Documents dir测试了这个bug,结果相同
但过了一会儿,我得到了这个日志输出:
无法向服务com.apple.WebKit.Networking发送信号:113:找不到指定的服务
这个答案表明,这可能与未将webview正确添加为子视图有关。但我看不出是否将webview添加到子视图时出错
更新
进一步调试视图层次结构会发现“wkwebview的位置不明确”
而po[[UIWindow keyWindow]\u 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行为吗
- 如果是这样,我如何解决这种含糊不清的问题
由于我可以解决不明确的布局,视图层次结构调试器很高兴-但不幸的是,这并没有解决webview的问题-它仍然没有响应loadFileURL请求!好的,最后我找到了解决方案: 我无意中创建了一个新的ViewController对象,而不是使用当前实例,并且我的代码在新的ViewController上初始化了另一个webview 因此,我不知何故丢失了对旧的可见webview的引用,因此它不再响应 在使用了正确的实例之后,一切都正常了。但是我仍然想知道为什么这并没有导致指向这个的其他错误