OC
- 獲取js中的key
NSString * jsCode = @"var arr = [1,2,3]";
JSContext * ctx = [[JSContext alloc] init];
[ctx evaluateScript:jsCode];
JSValue * jsArr = ctx[@"arr"];
jsArr[0] = @5;
NSLog(@"%@",jsArr); // 5 , 2 , 3
- OC調(diào)用js方法
NSString * jsCode = @"function hello(say){"
"return say ;"
"}";
JSContext * ctx = [[JSContext alloc] init];
[ctx evaluateScript:jsCode];
JSValue * hello = ctx[@"hello"];
JSValue * result = [hello callWithArguments:@[@"你好"]];
NSLog(@"%@",result); //你好
- JS調(diào)用OC中不帶參數(shù)的block
JSContext * ctx = [[JSContext alloc] init];
ctx[@"eat"] = ^(){
NSLog(@"eatsome");
};
NSString * jsCode = @"eat()";
[ctx evaluateScript:jsCode];
- JS調(diào)用OC中帶參數(shù)的block
JSContext * ctx = [[JSContext alloc] init];
ctx[@"eat"] = ^(){
NSArray * arguments = [JSContext currentArguments];
NSLog(@"eat%@",arguments[0]);
};
NSString * jsCode = @"eat('food')";
[ctx evaluateScript:jsCode];
- 通過(guò)JS調(diào)用OC自定義類(lèi)
Person * p = [[Person alloc] init];
p.name = @"zz";
JSContext * ctx = [[JSContext alloc] init];
ctx[@"person"] = p;
// NSString * jsCode = @"person.playGame('lol','night')";
NSString * jsCode = @"person.play()";
[ctx evaluateScript:jsCode];
Person文件.h
#import <JavaScriptCore/JavaScriptCore.h>
@protocol PersonJSExport <JSExport>
@property (nonatomic, strong)NSString * name;
- (void)play;
// 調(diào)用多個(gè)參數(shù)的方法滞谢,JS函數(shù)命名規(guī)則和OC還不一樣曹体,很可能調(diào)用不到對(duì)應(yīng)的JS生成的函數(shù)渐裂,為了保證生成的JS函數(shù)和OC方法名一致踩寇,OC提供了一個(gè)宏JSExportAs愿汰,用來(lái)告訴JS應(yīng)該生成什么樣的函數(shù)對(duì)應(yīng)OC的方法,這樣就不會(huì)調(diào)錯(cuò)了犁河。
// PropertyName:JS函數(shù)生成的名字
// JS就會(huì)自動(dòng)生成playGame這個(gè)方法
JSExportAs(playGame, - (void)playWithGame:(NSString *)game time:(NSString *)time);
@end
@interface Person : NSObject <PersonJSExport>
@property (nonatomic, strong) NSString *name;
- (void)playWithGame:(NSString *)game time:(NSString *)time;
Person.m
- (void)play {
NSLog(@"%@玩",_name);
}
- (void)playWithGame:(NSString *)game time:(NSString *)time
{
NSLog(@"%@在%@玩%@",_name,time,game);
}
- JS調(diào)用OC系統(tǒng)類(lèi)
@protocol UILabelJSExport <JSExport>
@property (nonatomic, strong)NSString * text;
@end
#pragma mark - JS調(diào)用OC系統(tǒng)類(lèi)
- (void)jsCallOCSystemClass {
class_addProtocol([UILabel class], @protocol(UILabelJSExport));
UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(50, 50, 100, 100)];
[self.view addSubview:label];
JSContext * ctx = [[JSContext alloc] init];
ctx[@"label"] = label;
NSString * jsCode = @"label.text = 'hello '";
[ctx evaluateScript:jsCode];
}
- js點(diǎn)擊事件調(diào)用原生方法
#import <JavaScriptCore/JavaScriptCore.h>
@protocol HXJSExport <JSExport>
- (void)ClickJsButton:(NSString *)jsonString;
@end
@interface ViewController : UIViewController <HXJSExport>
@property (strong, nonatomic) JSContext *context; //js交互的類(lèi)
@end
- (void)webViewDidFinishLoad:(UIWebView *)webView {
//原生套入JS
self.context = [webView valueForKeyPath:@"documentView.webView.mainFrame.javaScriptContext"];
// 以 JSExport 協(xié)議關(guān)聯(lián) native 的方法
self.context[@"Person"] = self;
}
- (void)ClickJsButton:(NSString *)jsonString{
NSLog(@"js回調(diào)");
}