插件自定义扩展

概要

插件是js调用原生能力的封装。IPU除了提供一套默认的插件API,同时也支持自定义的插件扩展,以满足复杂的业务需求。IPU团队希望能吸收各个团队的高质量插件,共同发展IPU框架,同时会给这些无私的贡献者适当的奖励。 如有分享,欢迎联系我们,邮箱:AICMC-CRM-IPUDept@asiainfo.com

1. 在js文件中定义API

  • 无需回调的插件
    call:function(sn,autoCall,err){
      if(autoCall==undefined){
          autoCall= true;//false-跳转至拨打界面,true-直接拨打
      }
      execute("call", [sn,autoCall],err);
    }
    
  • 需要回调的插件 需要存储回调时执行的js方法。
    storageCallback("getDate",callback);
    

注:框架自带插件在wade-mobile.js和expand-mobile.js中定义,业务扩展插件定义在biz-mobile.js中

2. 使用原生语言实现功能

Android

继承父类Plugin,实现如下方法:

public void call(JSONArray param) throws Exception {
    if(param.length()<2){
        throw new Exception(Messages.EXCEPTION_PARAM);
    }
    String sn = param.getString(0);
    String flag  = param.getString(1);
    call(sn, flag);
}

public void call(String sn,int flag) {
    if(flag==1){
        /**权限:android.permission.CALL_PHONE*/
        Intent intent = new Intent(Intent.ACTION_CALL,Uri.parse("tel:"+ sn));
        this.context.startActivity(intent);
    }else{
        /**Intent.ACTION_CALL|Intent.ACTION_DIAL*/
        Intent intent = new Intent(Intent.ACTION_DIAL,Uri.parse("tel:"+ sn));
        this.context.startActivity(intent);
    }
}

回调时需要调用父类Plugin中的callback方法,并回传结果。

this.callback(result);

iOS

继承父类WDPlugin,实现如下方法:

- (void)call:(NSArray *)param {
    id num = [[param objectAtIndex:0] copy];
    BOOL re = [self callPhone:num];
    if (re) {
      [self callback:@"call success"];
    } else {
      [self error:@"call failed"];
    }
}

- (BOOL)callPhone:(NSString *)num {
    BOOL re = [self openUrl:[NSString stringWithFormat:@"tel://%@", num]];
    return re;
}

回调时需要调用父类WDPlugin中的callback方法,并回传结果。

[self callback:result];

注:为什么有实现功能时有两个call方法,参数类型为JSONArray的方法是提供给js映射的,另外一个方法可以提供给原生代码直接使用,如:

MobileBasic plugin = wademobile.getPluginManager().getPlugin(MobileBasic.class);
plugin.call("13712341234", true);

3. 在配置文件中注册

android

客户端工程assets/mobile-action.xml中

<action name="call" class="com.wade.mobile.func.MobileBasic" method="call"/>

ios

客户端工程Res/config/mobile-action.xml中

<action name="call" class="WDFMobileBasic" method="call"/>

4. 在客户端程序中测试

    WadeMobile.call("13712341234", true);
Copyright © aiipu.com 2017 all right reserved,powered by Gitbook该文件修订时间: 2020-09-01 15:11:34

results matching ""

    No results matching ""