//定义需要暴露给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)testXXX
{
JSContext *context = [[JSContext alloc] init];
//创建Person类的对象,将他赋值给js对象
Person *person=[Person new];
person.personName = @"Greg";
person.age = 27;
context[@"person"]=person;
JSValue *kValue = context[@"person"];
//可以调用获取PersonProtocol暴露的内容
NSString *personName = [[context evaluateScript:@"person.personName"] toString]; //"Greg"
NSString *personName1 = [[context evaluateScript:@"person.queryPersonName()"] toString]; //"Greg"
//js无法调用跟age相关的内容
NSInteger age = [[context evaluateScript:@"person.age"] toInt32]; // 0
NSInteger age1 = [[context evaluateScript:@"person.queryPersonAge()"] toInt32]; //0
}
* thread #1: tid = 0x15f9d0, 0x0000000109bfcb00 jsContextTest`-[Person queryPersonName](self=0x00007fc74be117f0, _cmd="queryPersonName") + 16 at ViewController.m:28, queue = 'com.apple.main-thread', stop reason = breakpoint 1.1
* frame #0: 0x0000000109bfcb00 jsContextTest`-[Person queryPersonName](self=0x00007fc74be117f0, _cmd="queryPersonName") + 16 at ViewController.m:28
frame #1: 0x000000010a57a5cc CoreFoundation`__invoking___ + 140
frame #2: 0x000000010a57a41e CoreFoundation`-[NSInvocation invoke] + 286
frame #3: 0x000000010ae0adb3 JavaScriptCore`JSC::ObjCCallbackFunctionImpl::call(JSContext*, OpaqueJSValue*, unsigned long, OpaqueJSValue const* const*, OpaqueJSValue const**) + 451
frame #4: 0x000000010ae0a926 JavaScriptCore`JSC::objCCallbackFunctionCallAsFunction(OpaqueJSContext const*, OpaqueJSValue*, OpaqueJSValue*, unsigned long, OpaqueJSValue const* const*, OpaqueJSValue const**) + 262
frame #5: 0x000000010ae0bbad JavaScriptCore`long long JSC::APICallbackFunction::call<JSC::ObjCCallbackFunction>(JSC::ExecState*) + 573
frame #6: 0x000000010add5340 JavaScriptCore`JSC::LLInt::setUpCall(JSC::ExecState*, JSC::Instruction*, JSC::CodeSpecializationKind, JSC::JSValue, JSC::LLIntCallLinkInfo*) + 528
frame #7: 0x000000010addc35d JavaScriptCore`llint_entry + 22900
frame #8: 0x000000010add67d9 JavaScriptCore`vmEntryToJavaScript + 326
frame #9: 0x000000010aca8959 JavaScriptCore`JSC::JITCode::execute(JSC::VM*, JSC::ProtoCallFrame*) + 169
frame #10: 0x000000010ac8f264 JavaScriptCore`JSC::Interpreter::execute(JSC::ProgramExecutable*, JSC::ExecState*, JSC::JSObject*) + 10404
frame #11: 0x000000010aa9f786 JavaScriptCore`JSC::evaluate(JSC::ExecState*, JSC::SourceCode const&, JSC::JSValue, WTF::NakedPtr<JSC::Exception>&) + 470
frame #12: 0x000000010acedfb8 JavaScriptCore`JSEvaluateScript + 424
frame #13: 0x000000010acfcd09 JavaScriptCore`-[JSContext evaluateScript:withSourceURL:] + 105
frame #14: 0x0000000109bfd3f2 jsContextTest`-[ViewController testXXX](self=0x00007fc74bd4e4e0, _cmd="testXXX") + 370 at ViewController.m:91
frame #15: 0x000000010b189a8d UIKit`-[UIApplication sendAction:to:from:forEvent:] + 92
frame #16: 0x000000010b2fce67 UIKit`-[UIControl sendAction:to:forEvent:] + 67
frame #17: 0x000000010b2fd143 UIKit`-[UIControl _sendActionsForEvents:withEvent:] + 327
frame #18: 0x000000010b2fc263 UIKit`-[UIControl touchesEnded:withEvent:] + 601
frame #19: 0x000000010b1fc99f UIKit`-[UIWindow _sendTouchesForEvent:] + 835
frame #20: 0x000000010b1fd6d4 UIKit`-[UIWindow sendEvent:] + 865
frame #21: 0x000000010b1a8dc6 UIKit`-[UIApplication sendEvent:] + 263
frame #22: 0x000000010b182553 UIKit`_UIApplicationHandleEventQueue + 6660
frame #23: 0x000000010a5b7301 CoreFoundation`__CFRUNLOOP_IS_CALLING_OUT_TO_A_SOURCE0_PERFORM_FUNCTION__ + 17
frame #24: 0x000000010a5ad22c CoreFoundation`__CFRunLoopDoSources0 + 556
frame #25: 0x000000010a5ac6e3 CoreFoundation`__CFRunLoopRun + 867
frame #26: 0x000000010a5ac0f8 CoreFoundation`CFRunLoopRunSpecific + 488
frame #27: 0x000000010e68cad2 GraphicsServices`GSEventRunModal + 161
frame #28: 0x000000010b187f09 UIKit`UIApplicationMain + 171
frame #29: 0x0000000109bfd90f jsContextTest`main(argc=1, argv=0x00007fff56003638) + 111 at main.m:14
frame #30: 0x000000010d5c292d libdyld.dylib`start + 1
frame #31: 0x000000010d5c292d libdyld.dylib`start + 1