我正在使用链接中引入的UIWebView
和 JSExport 构建应用:
https://www.bignerdranch.com/blog/javascriptcore-example/
MyConsole代码:
@objc protocol ConsoleJSExports : JSExport {
static func log(message: String) -> Void
static func error(message: String) -> Void
}
@objc public class MyConsole : NSObject, ConsoleJSExports {
public class func log(message: String) {
print("JS Log:\(message)")
}
public class func error(message: String) {
print("JS Error:\(message)")
}
MyClass代码:
extension MyClass: UIWebViewDelegate {
func webView(webView: UIWebView, shouldStartLoadWithRequest request: NSURLRequest, navigationType: UIWebViewNavigationType) -> Bool {
return true
}
func webViewDidFinishLoad(webView: UIWebView) {
weak var context = webView!.valueForKeyPath("documentView.webView.mainFrame.javaScriptContext") as? JSContext
context?.setObject(MyConsole.self, forKeyedSubscript: "console")
}
}
此应用程序运行正常,但在我将其删除并多次启动后,当我再次尝试访问webView的上下文时,它会崩溃:
func initMyJSCode(myId: String, rootUrl: String) {
let context = webView.valueForKeyPath("documentView.webView.mainFrame.javaScriptContext") as! JSContext
context.objectForKeyedSubscript("initMyJSCode").callWithArguments([myId, rootUrl])
}
我遇到错误:
Exception Type: EXC_BAD_ACCESS (SIGSEGV)
Exception Subtype: KERN_INVALID_ADDRESS at 0x000000010a58beb8
Triggered by Thread: 6
Thread 0 name: Dispatch queue: com.apple.main-thread
Thread 0:
0 JavaScriptCore 0x00000001832f36b8 JSC::BytecodeGenerator::BytecodeGenerator(JSC::VM&, JSC::FunctionBodyNode*, JSC::UnlinkedFunctionCodeBlock*, JSC::DebuggerMode, JSC::ProfilerMode) + 4712
1 JavaScriptCore 0x00000001832f32d4 JSC::BytecodeGenerator::BytecodeGenerator(JSC::VM&, JSC::FunctionBodyNode*, JSC::UnlinkedFunctionCodeBlock*, JSC::DebuggerMode, JSC::ProfilerMode) + 3716
2 JavaScriptCore 0x0000000183595250 JSC::UnlinkedFunctionExecutable::codeBlockFor(JSC::VM&, JSC::SourceCode const&, JSC::CodeSpecializationKind, JSC::DebuggerMode, JSC::ProfilerMode, bool, JSC::ParserError&) + 468
3 JavaScriptCore 0x00000001833e8794 JSC::ScriptExecutable::newCodeBlockFor(JSC::CodeSpecializationKind, JSC::JSFunction*, JSC::JSScope**, JSC::JSObject*&) + 628
4 JavaScriptCore 0x00000001833e8d40 JSC::ScriptExecutable::prepareForExecutionImpl(JSC::ExecState*, JSC::JSFunction*, JSC::JSScope**, JSC::CodeSpecializationKind) + 120
5 JavaScriptCore 0x00000001831d10bc JSC::LLInt::setUpCall(JSC::ExecState*, JSC::Instruction*, JSC::CodeSpecializationKind, JSC::JSValue, JSC::LLIntCallLinkInfo*) + 600
6 JavaScriptCore 0x000000018350204c llint_entry + 24636
7 JavaScriptCore 0x0000000183502058 llint_entry + 24648
8 JavaScriptCore 0x0000000183502058 llint_entry + 24648
9 JavaScriptCore 0x0000000183502058 llint_entry + 24648
10 JavaScriptCore 0x0000000183502058 llint_entry + 24648
11 JavaScriptCore 0x0000000183502058 llint_entry + 24648
12 JavaScriptCore 0x0000000183502058 llint_entry + 24648
13 JavaScriptCore 0x0000000183502058 llint_entry + 24648
14 JavaScriptCore 0x0000000183502058 llint_entry + 24648
15 JavaScriptCore 0x0000000183502058 llint_entry + 24648
16 JavaScriptCore 0x0000000183502058 llint_entry + 24648
17 JavaScriptCore 0x0000000183502058 llint_entry + 24648
18 JavaScriptCore 0x0000000183502058 llint_entry + 24648
19 JavaScriptCore 0x00000001834fbdf0 callToJavaScript + 304
20 JavaScriptCore 0x000000018346a454 JSC::JITCode::execute(JSC::VM*, JSC::ProtoCallFrame*) + 44
21 JavaScriptCore 0x00000001831d5554 JSC::Interpreter::executeCall(JSC::ExecState*, JSC::JSObject*, JSC::CallType, JSC::CallData const&, JSC::JSValue, JSC::ArgList const&) + 416
22 JavaScriptCore 0x00000001831d52e0 JSObjectCallAsFunction + 556
23 JavaScriptCore 0x00000001834d8964 -[JSValue callWithArguments:] + 288
有人可以帮忙吗?
答案 0 :(得分:0)
我发现从UIWebView获取JSContext并不是一个好主意。由于它不是公共API,因此无法保证在运行时始终可以访问JSContext。