JS 调用OC 不带参数的写法:
JS 方法里面调用OC
jsb.reflection.callStaticMethod("GameFacade", "openWeChatApp");
OC 方法的写法:(OC 必须是静态方法,所谓的+方法)
+ (void)openWeChatApp {
[WXApi openWXApp];
}
JS 调用OC 带参数的写法:(当前参数只能支持 BOOL/ Float/NSString)
JS 方法里面调用OC
distance = jsb.reflection.callStaticMethod("GameFacade", "calDistance:startLongitude:endLatitude:endLongitude:",startLatitude,startLongitude,endLatitude,endLongitude);
OC 方法的写法:
//计算距离
+ (NSNumber *)calDistance:(NSNumber *)startLatitude startLongitude:(NSNumber *)startLongitude endLatitude:(NSNumber *)endLatitude endLongitude:(NSNumber *)endLongitude {
DebugLog(@"startLatitude = %f,startLongitude = %f,endLatitude = %f, endLongitude = %f",[startLatitude doubleValue],[startLongitude doubleValue],[endLatitude doubleValue],[endLongitude doubleValue]);
AppController *delegateNew = (AppController *)[UIApplication sharedApplication].delegate;
RootViewController *rootCtl = (RootViewController *) delegateNew.viewController;
return @([rootCtl calDistance:[startLatitude doubleValue] andStartLongitude:[startLongitude doubleValue] andEndLatitude:[endLatitude doubleValue] andLongitude:[endLongitude doubleValue]]);
}
OC 调JS的方法:
#include "cocos/scripting/js-bindings/jswrapper/SeApi.h"
se::ScriptEngine::getInstance()->evalString("cc.ww.AppInterface.vibrate(1,1);");
cc.ww.AppInterface:JS的文件名称。
vibrate(1,1):JS 文件里面的方法名及2个int参数。
JS 文件方法定义如下:(JS 的方法也都是 statics 方法)
// 振动, milliseconds振动时长, repeat 重复振动次数
vibrate: function (milliseconds, repeatCnt) {
if (!cc.sys.isNative) {
return;
}
if (cc.sys.os == cc.sys.OS_ANDROID) {
jsb.reflection.callStaticMethod("com.kekehappy.kkmh.GameFacade", "vibrate", "(II)V", milliseconds, repeatCnt);
} else if (cc.sys.os == cc.sys.OS_IOS) {
jsb.reflection.callStaticMethod("GameFacade", "callIphoneShake");
}
cc.log("vibrate");
},