xcode 什么是 WKWebView 中的 WKErrorDomain 错误 4
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/27953374/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
What is WKErrorDomain error 4 from WKWebView
提问by donkey
fatal error: LPWebView encounters an error: Error Domain=WKErrorDomain Code=4
"A JavaScript exception occurred" UserInfo=0x79d9c700
{NSLocalizedDescription=A JavaScript exception occurred}
I encountered this error when I tried to evaluate a JavaScript function with WKWebView.
当我尝试使用 WKWebView 评估 JavaScript 函数时遇到此错误。
I used loadHTMLString
to load a template to the webview.
我曾经loadHTMLString
将模板加载到 webview。
let bundle = NSBundle.mainBundle()
if let editorURL = bundle.URLForResource(self.kTemplateName,
withExtension: "html") {
var error : NSError?
//get html string from editor.html
if let htmlString = String(contentsOfURL: editorURL, encoding: NSUTF8StringEncoding, error: &error){
if error != nil {
assertionFailure("error encountered reading html string for \(error)")
} else {
self.loadHTMLString(htmlString, baseURL: bundle.bundleURL)
}
}
} else {
assertionFailure("LPWebView template not found")
}
I wonder what this error code means and how to solve it?
我想知道这个错误代码是什么意思以及如何解决它?
Thank you very much!
非常感谢!
回答by Max MacLeod
So if we dig into the headers:
因此,如果我们深入研究标题:
/*! @constant WKErrorDomain Indicates a WebKit error. */
@availability(iOS, introduced=8.0)
let WKErrorDomain: String
/*! @enum WKErrorCode
@abstract Constants used by NSError to indicate errors in the WebKit domain.
@constant WKErrorUnkcnown Indicates that an unknown error occurred.
@constant WKErrorWebContentProcessTerminated Indicates that the Web Content process was terminated.
@constant WKErrorWebViewInvalidated Indicates that the WKWebView was invalidated.
@constant WKErrorJavaScriptExceptionOccurred Indicates that a JavaScript exception occurred.
*/
@availability(iOS, introduced=8.0)
enum WKErrorCode : Int {
case Unknown
case WebContentProcessTerminated
case WebViewInvalidated
case JavaScriptExceptionOccurred
}
Error code 4 would corresponds to JavaScriptExceptionOccurred
, or WKErrorJavaScriptExceptionOccurred
.
错误代码 4 对应于JavaScriptExceptionOccurred
, 或WKErrorJavaScriptExceptionOccurred
。
In other words, the JavaScript function causes some error.
换句话说,JavaScript 函数会导致一些错误。
Probably not more here that you couldn't guess already. For resolution, I would suggest using the developer features of a web browser such as Safari, loading the HTML and debugging.
可能不会更多,你已经猜不到了。对于解决方案,我建议使用 Web 浏览器(例如 Safari)的开发人员功能、加载 HTML 和调试。
In fact, as explained in the WWDC 2014 video, "Introducing the Modern WebKit API", your desktop Safari browser can "inspect the WKWebView
using the Safari web inspector, including any user scripts that you've injected."
事实上,正如WWDC 2014 视频“Introducing the Modern WebKit API”中所解释的那样,您的桌面 Safari 浏览器可以“检查WKWebView
使用 Safari Web 检查器的情况,包括您注入的任何用户脚本”。
To use this, while the WKWebView
is loaded in memory in your running app on the iOS simulator, open the desktop Safari browser and access Developon the top menu bar then iOS Simulator. That will show a drop down of the web view's document objects.
要使用它,当WKWebView
iOS 模拟器上正在运行的应用程序的内存中加载时,打开桌面 Safari 浏览器并访问顶部菜单栏上的Develop,然后访问iOS Simulator。这将显示 Web 视图的文档对象的下拉列表。
For more info on debugging JavaScript, take a look at Web Inspector: Understanding Stack Traces
有关调试 JavaScript 的更多信息,请查看Web Inspector:Understanding Stack Traces