iOS原生&JS交互

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调用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调用OC中不带参数的block
    JSContext * ctx = [[JSContext alloc] init];
    
    ctx[@"eat"] = ^(){
        NSLog(@"eatsome");
    };
    NSString * jsCode = @"eat()";
    
    [ctx evaluateScript:jsCode];
  • JS调用OC中带参数的block
    JSContext * ctx = [[JSContext alloc] init];
    ctx[@"eat"] = ^(){
        NSArray * arguments = [JSContext currentArguments];
        NSLog(@"eat%@",arguments[0]);
    };
    
    NSString * jsCode = @"eat('food')";
    [ctx evaluateScript:jsCode];
  • 通过JS调用OC自定义类
    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;

// 调用多个参数的方法,JS函数命名规则和OC还不一样,很可能调用不到对应的JS生成的函数,为了保证生成的JS函数和OC方法名一致,OC提供了一个宏JSExportAs,用来告诉JS应该生成什么样的函数对应OC的方法,这样就不会调错了。

// PropertyName:JS函数生成的名字
// JS就会自动生成playGame这个方法
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调用OC系统类
@protocol UILabelJSExport <JSExport>

@property (nonatomic, strong)NSString * text;

@end
#pragma mark - JS调用OC系统类
- (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点击事件调用原生方法
#import <JavaScriptCore/JavaScriptCore.h>


@protocol HXJSExport <JSExport>
- (void)ClickJsButton:(NSString *)jsonString;
@end

@interface ViewController : UIViewController <HXJSExport>

@property (strong, nonatomic) JSContext *context; //js交互的类

@end

- (void)webViewDidFinishLoad:(UIWebView *)webView {
    //原生套入JS
    self.context = [webView valueForKeyPath:@"documentView.webView.mainFrame.javaScriptContext"];
    // 以 JSExport 协议关联 native 的方法
    self.context[@"Person"] = self;
}
- (void)ClickJsButton:(NSString *)jsonString{
    NSLog(@"js回调");
}
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

  • 项目中涉及OC与网页的交互,查找资料时看到了JavaScriptCore.framework,就对照文章ios7 ...
    YaoYaoX阅读 2,503评论 7 11
  • 一、JavaScriptCore常用的类 JavaScriptCore作用:JavaScriptCore是苹果原生...
    CoderZS阅读 933评论 0 8
  • 随着H5技术的兴起,在iOS开发过程中,难免会遇到原生应用需要和H5页面交互的问题。其中会涉及方法调用及参数传值等...
    Chris_js阅读 3,261评论 1 8
  • 跟原生开发相比,H5的开发相对来一个成熟的框架和团队来讲在开发速度和开发效率上有着比原生很大的优势,至少不用等待审...
    大冲哥阅读 1,909评论 0 7
  • 都说爱笑的女孩儿运气不会差,但在脂肪面前,显然……人!人!平!等! “大笑姑婆”吴君如也不例外。 不过据说吴君如从...
    减约阅读 2,070评论 0 0

友情链接更多精彩内容