分属不同的线程
//定义需要暴露给js的内容,这里我们只暴露personName和queryPersonName接口
@protocol PersonProtocol <JSExport>
@property(nonatomic,copy)NSString *personName;
-(NSString *)queryPersonName;
@end
//Person实现PersonProtocol协议,而自己定义的age和queryPersonAge接口不暴露给js
@interface Person : NSObject <PersonProtocol>
@property(nonatomic,assign)NSInteger age;
-(NSInteger)queryPersonAge;
@end
@implementation Person
@synthesize personName = _personName;
-(NSString *)queryPersonName{
return self.personName;
}
-(NSInteger)queryPersonAge{
return self.age;
}
@end
-(void)refreshXX
{
NSString *path = [[NSBundle mainBundle] pathForResource:@"info" ofType:@"html"];
NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL fileURLWithPath:path]];
[self.webView loadRequest:request];
JSContext *jsContext = [self.webView valueForKeyPath:@"documentView.webView.mainFrame.javaScriptContext"];
Person *person=[Person new];
person.personName = @"Greg";
person.age = 27;
jsContext[@"person"]=person;
}
<div data-role="collapsible">
<h3 onclick="person.queryPersonName()">systemVersion</h3>
<p id="systemVersion"></p>
</div>
(lldb) bt
* thread #1: tid = 0x16b9e9, 0x000000010d5c1d40 jsTest`-[ViewController refreshXX](self=0x00007ff12a60ea30, _cmd="refreshXX") + 16 at ViewController.m:63, queue = 'com.apple.main-thread', stop reason = breakpoint 2.1
* frame #0: 0x000000010d5c1d40 jsTest`-[ViewController refreshXX](self=0x00007ff12a60ea30, _cmd="refreshXX") + 16 at ViewController.m:63
frame #1: 0x000000010eb4ea8d UIKit`-[UIApplication sendAction:to:from:forEvent:] + 92
frame #2: 0x000000010ecc1e67 UIKit`-[UIControl sendAction:to:forEvent:] + 67
frame #3: 0x000000010ecc2143 UIKit`-[UIControl _sendActionsForEvents:withEvent:] + 327
frame #4: 0x000000010ecc1263 UIKit`-[UIControl touchesEnded:withEvent:] + 601
frame #5: 0x000000010ebc199f UIKit`-[UIWindow _sendTouchesForEvent:] + 835
frame #6: 0x000000010ebc26d4 UIKit`-[UIWindow sendEvent:] + 865
frame #7: 0x000000010eb6ddc6 UIKit`-[UIApplication sendEvent:] + 263
frame #8: 0x000000010eb47553 UIKit`_UIApplicationHandleEventQueue + 6660
frame #9: 0x000000010e6bb301 CoreFoundation`__CFRUNLOOP_IS_CALLING_OUT_TO_A_SOURCE0_PERFORM_FUNCTION__ + 17
frame #10: 0x000000010e6b122c CoreFoundation`__CFRunLoopDoSources0 + 556
frame #11: 0x000000010e6b06e3 CoreFoundation`__CFRunLoopRun + 867
frame #12: 0x000000010e6b00f8 CoreFoundation`CFRunLoopRunSpecific + 488
frame #13: 0x0000000112051ad2 GraphicsServices`GSEventRunModal + 161
frame #14: 0x000000010eb4cf09 UIKit`UIApplicationMain + 171
frame #15: 0x000000010d5c277f jsTest`main(argc=1, argv=0x00007fff5263e680) + 111 at main.m:14
frame #16: 0x00000001105b292d libdyld.dylib`start + 1
2017-04-07 14:50:16.620 jsTest[31840:1489385] /Users/zzf073/Library/Developer/CoreSimulator/Devices/96C38E7B-35FF-49AC-80A1-BCAA1E8E7873/data/Containers/Bundle/Application/AFCD9E1B-D5FD-498E-8AD5-AEDFC672314F/jsTest.app/info.html
(lldb) bt
* thread #8: tid = 0x16ba34, 0x000000010d5c1770 jsTest`-[Person queryPersonName](self=0x00007ff12a61ca20, _cmd="queryPersonName") + 16 at ViewController.m:28, name = 'WebThread', stop reason = breakpoint 1.1
* frame #0: 0x000000010d5c1770 jsTest`-[Person queryPersonName](self=0x00007ff12a61ca20, _cmd="queryPersonName") + 16 at ViewController.m:28
frame #1: 0x000000010e67e5cc CoreFoundation`__invoking___ + 140
frame #2: 0x000000010e67e41e CoreFoundation`-[NSInvocation invoke] + 286
frame #3: 0x000000010da38db3 JavaScriptCore`JSC::ObjCCallbackFunctionImpl::call(JSContext*, OpaqueJSValue*, unsigned long, OpaqueJSValue const* const*, OpaqueJSValue const**) + 451
frame #4: 0x000000010da38926 JavaScriptCore`JSC::objCCallbackFunctionCallAsFunction(OpaqueJSContext const*, OpaqueJSValue*, OpaqueJSValue*, unsigned long, OpaqueJSValue const* const*, OpaqueJSValue const**) + 262
frame #5: 0x000000010da39bad JavaScriptCore`long long JSC::APICallbackFunction::call<JSC::ObjCCallbackFunction>(JSC::ExecState*) + 573
frame #6: 0x000000010da03340 JavaScriptCore`JSC::LLInt::setUpCall(JSC::ExecState*, JSC::Instruction*, JSC::CodeSpecializationKind, JSC::JSValue, JSC::LLIntCallLinkInfo*) + 528
frame #7: 0x000000010da0a35d JavaScriptCore`llint_entry + 22900
frame #8: 0x000000010da047d9 JavaScriptCore`vmEntryToJavaScript + 326
frame #9: 0x000000010d8d6959 JavaScriptCore`JSC::JITCode::execute(JSC::VM*, JSC::ProtoCallFrame*) + 169
frame #10: 0x000000010d8bd5ad JavaScriptCore`JSC::Interpreter::executeCall(JSC::ExecState*, JSC::JSObject*, JSC::CallType, JSC::CallData const&, JSC::JSValue, JSC::ArgList const&) + 493
frame #11: 0x000000010d691bd7 JavaScriptCore`JSC::call(JSC::ExecState*, JSC::JSValue, JSC::CallType, JSC::CallData const&, JSC::JSValue, JSC::ArgList const&, WTF::NakedPtr<JSC::Exception>&) + 71
frame #12: 0x000000011395fc1a WebCore`WebCore::JSEventListener::handleEvent(WebCore::ScriptExecutionContext*, WebCore::Event*) + 1002
frame #13: 0x00000001135ac630 WebCore`WebCore::EventTarget::fireEventListeners(WebCore::Event*, WebCore::EventTargetData*, WTF::Vector<WebCore::RegisteredEventListener, 1ul, WTF::CrashOnOverflow, 16ul>&) + 752
frame #14: 0x00000001135ac267 WebCore`WebCore::EventTarget::fireEventListeners(WebCore::Event*) + 311
frame #15: 0x000000011359550e WebCore`WebCore::MouseOrFocusEventContext::handleLocalEvents(WebCore::Event&) const + 174
frame #16: 0x00000001135960b8 WebCore`WebCore::EventDispatcher::dispatchEvent(WebCore::Node*, WTF::PassRefPtr<WebCore::Event>) + 920
frame #17: 0x0000000113c97c5d WebCore`WebCore::Node::dispatchEvent(WTF::PassRefPtr<WebCore::Event>) + 29
frame #18: 0x000000011357f61e WebCore`WebCore::Element::dispatchMouseEvent(WebCore::PlatformMouseEvent const&, WTF::AtomicString const&, int, WebCore::Element*) + 318
frame #19: 0x000000011359c8df WebCore`WebCore::EventHandler::dispatchMouseEvent(WTF::AtomicString const&, WebCore::Node*, bool, int, WebCore::PlatformMouseEvent const&, bool) + 111
frame #20: 0x000000011359e108 WebCore`WebCore::EventHandler::handleMouseReleaseEvent(WebCore::PlatformMouseEvent const&) + 1192
frame #21: 0x00000001135a550d WebCore`WebCore::EventHandler::mouseUp(WebEvent*) + 237
frame #22: 0x000000011310cbe3 WebKitLegacy`-[WebHTMLView mouseUp:] + 115
frame #23: 0x000000011414c30d WebCore`-[WAKView _selfHandleEvent:] + 125
frame #24: 0x000000011414c26f WebCore`-[WAKView handleEvent:] + 63
frame #25: 0x000000011414f46d WebCore`-[WAKWindow sendEventSynchronously:] + 269
frame #26: 0x000000010ee6ce81 UIKit`__47-[UIWebDocumentView(Interaction) performClick:]_block_invoke + 740
frame #27: 0x000000010ee6cb97 UIKit`-[UIWebDocumentView(Interaction) performClick:] + 68
frame #28: 0x000000010ee6d49f UIKit`__64-[UIWebDocumentView(Interaction) _sendMouseMoveAndAttemptClick:]_block_invoke + 345
frame #29: 0x0000000114167996 WebCore`HandleRunSource(void*) + 390
frame #30: 0x000000010e6bb301 CoreFoundation`__CFRUNLOOP_IS_CALLING_OUT_TO_A_SOURCE0_PERFORM_FUNCTION__ + 17
frame #31: 0x000000010e6b11a7 CoreFoundation`__CFRunLoopDoSources0 + 423
frame #32: 0x000000010e6b06e3 CoreFoundation`__CFRunLoopRun + 867
frame #33: 0x000000010e6b00f8 CoreFoundation`CFRunLoopRunSpecific + 488
frame #34: 0x0000000114167325 WebCore`RunWebThread(void*) + 469
frame #35: 0x00000001108d199d libsystem_pthread.dylib`_pthread_body + 131
frame #36: 0x00000001108d191a libsystem_pthread.dylib`_pthread_start + 168
frame #37: 0x00000001108cf351 libsystem_pthread.dylib`thread_start + 13