1
訪問JSContext
我建立使用UIWebView
和JSExport在鏈路引入到應用程序: https://www.bignerdranch.com/blog/javascriptcore-example/EXC_BAD_ACCESS當UIWebView的
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
任何人都可以請幫助?
有幾件事。 1)你能發佈在github上崩潰的代碼版本嗎? 2)您在webViewDidFinishLoad中不必要地將上下文變爲一個弱變量,如果可能的話避免變弱。 3)JavaScriptCore比Objective-C更容易使用Swift。 –