apicloud 模块开发提供了 js 与底层交互的接口,官方的 sdk 用于方便开发模块,总结一下与 js 交互常用的方法

读取接口参数

引入NSDirectionaryUtil.h头文件

- (void)printLog:(NSDirectionary *) paramsDict_ {
NSString *imagePath = [paramsDirect_ stringValueForKey:@"image" defaultValue:(NSString *)];
NSLog(@"params %@", imagePath);
}

文件读取

NSString *imageRealPath = [self getPathWithUZSchemaURL:imagePath];
NSLog(@"imageRealPath: %@", imagePath);

获取图片显示在当前页面

UIImage *image = [UIImage imageWithContentsOfFile:imageRealPath];
UIImage *imageview = [[UIImageView alloc]initWithImage:image];
imageview.frame = CGRectMake(0,0,320,300);
[self addSubview:imageview fixedOn:nil fixed:YES];

模块默认图片

在模块下面新建 res_moduleDemo 文件夹,获取 res_moduleDemo 文件下面图片路径

// res_moduleDemo/test.png
NSString *defaultImgPath = [[NSBundle mainBundle]pathForResource:@"res_moduleDemo/test", ofType:@"png"];

js 回调原生实现

// UZModuleDemo.m
@interface UZModuleDemo () {
NSInteger canBackCbid;
int count;
}
@end
// 初始化操作
- (id)initWithUZWebView:(id)webView {
self = [super initWithUZWebView:webView];
if (self != nil) {
canBackCbid = -1;
}
return self;
}
// js接口
-(void)canBack:(NSDirectionary *)paramsDict_ {
canBackCbid = [paramsDict_ integerValueForKey:@"cbId" defaultValue:-1];
// 定时器
[NSTimer scheduleTimerWithTimerInterval:1 target:self selector:@selector(continuouCallback) userInfo:nil repeats:YES];
}
// 定义回调函数
-(void)continuouCallback {
if (canBackCbid >= 0) {
NSMutableDictionary *sendDict = [NSMutableDictionary dictionary];
NSString *msgStr = [NSString stringWithFormat:@"计数: %d", count];
count ++;
// 设置对象 { msg: msgStr }
[sendDict setObject:msgStr forKey:@"msg"];
[self sendResultEventWithCallbackId:canBackCbid dataDict:sendDict errDict:nil doDelete:NO];
}
}

js 调用方法

var moduleDemo = api.require("moduleDemo");
moduleDemo.canBack(function(ret, err) {
alert(ret.msg);
})

模块生命周期

// 模块第一次调用,调用初始化函数
- (id)initWithUZWebView:(id)webView;
// 页面关闭,释放函数
- (void)dispose;

原生向前端发数据

两种实现方法,一种是直接声明函数,一种是监听

function nativeMessage(ret) {
alert(JSON.stringify(ret));
}
api.addEventListener(
{
name: 'nativeEvent',
},
function (ret, err) {
alert(JSON.stringify(ret));
}
);

原生代码向前端发数据

- (void)sendMessageByEvaljs:(NSDictionary *)paramsDict_ {
// 发送自定义事件
[self sendCustomEvent:@"nativeEvent" extra:@"{ 'content': 'hello' }"];
return;
// 直接发送事件给nativeMessage
NSString *jsStr = @"nativeMessage({ 'count': '参数' })";
[self evalJs:jsStr];
}

appDelegate 实现,使用第三方 sdk 初始化操作

// MyAppDelegate.h头文件
#import <Foundation/Foundation.h>
#import "UZAppDelegate.h"
@interface MyAppDelegate: NSObject
<UIApplicationDelgate>
@end
// MyAppDelegate.m文件
#import "MyAppDelegate.h"
@implementation MyAppDelegate
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
NSLog(@"didFinishLaunchingWithOptions");
return YES;
}
@end

在模块入口引入,在模块静态方法内调用 addAppHandle 来执行

#import "MyApplication.h"
+ (void)launch {
MyAppDelegate *appDele = [[MyAppDelegate alloc]init];
[theApp addAppHandle:appDele];
}

注:模块静态方法在 module.json 定义

{
"name": "moduleDemo",
"class": "UZModuleDemo",
"methods": ["printLog"],
"launchClassMethod": "launch"
}

读取 config.xml 文件

在 widget 包定义的config.xml文件如下

<feature name="wx">
<param name="url" value="wx213123213213" />
</feature>
NSDictionary *moduleInfo = [self getFeatureByName:@"wx"];
NSString *moduleKey = [moduleInfo stringValueForKey:@"url" defaultValue:@""];

读取加密的配置文件

存放在 widget 包下res/key.xml定义如下

<security>
<item name="wxPay_appId" value="wx12424123sadsakj"/>
</security>
NSString *wxPayAppid = [self securityValueForKey:@"wxPay_appId"];