美文网首页
weex 与原生的交互

weex 与原生的交互

作者: sujeking | 来源:发表于2019-03-20 12:00 被阅读0次

之前发现js调用原生方法比较好,但是今天发现我要调用js方法怎么弄呢,各种搜索【就是百度 =_=!】都是一毛一样的复制粘贴,哎,自己处理一下总结总结,当做一个笔记吧

Native ->JS

  • iOS
-(void)someAction {
    NSDictionary * dict = @{...}
   //传一个dict到weex
    [weexInstance fireGlobalEvent:@"methodName" params:dic];
}
  • weex
var globalEvent = require('@weex-module/globalEvent');
globalEvent.addEventListener("methodName",function(e){
    //your code
});

JS->Native

  • iOS
@implementation WXEventModule (SKAddTagVC) //添加一个扩展、也可以创建一个类

WX_EXPORT_METHOD(@selector(addTagVC:)) //定义要暴露的方法
WX_EXPORT_METHOD(@selector(addCustomTag))

//实现方法 [weexInstance 就是当前页面缓存起来的WXSDKInstance 对象]
- (void)addTagVC:(NSDictionary *)dict {
    SKAddTagVC *vc = (SKAddTagVC *)self.weexInstance.viewController;
    [vc netAddTagForMember:dict];
}

- (void)addCustomTag {
    SKAddTagVC *vc = (SKAddTagVC *)self.weexInstance.viewController;
    [vc loadTextView];
}

  • weex
weex.requireModule("event").loadAddTagVC(); 
weex.requireModule("event").callPhone(this.mainInfo["phoneNum"]);    //带参数

相关文章

网友评论

      本文标题:weex 与原生的交互

      本文链接:https://www.haomeiwen.com/subject/yzlzmqtx.html