JS里面調(diào)用OC
var ret = jsb.reflection.callStaticMethod("NativeOcClass",
"callNativeUIWithTitle:andContent:", "cocos2d-js",
"Yes! you call a Native UI from Reflection");
對(duì)應(yīng)的Oc代碼為:
+(BOOL)callNativeUIWithTitle:(NSString *) title andContent:(NSString *)content{
UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:title message:content delegate:self cancelButtonTitle:@"Cancel" otherButtonTitles:@"OK", nil];
[alertView show];
return true;
}
注意
在OC的實(shí)現(xiàn)中,如果方法的參數(shù)需要使用float货抄、int述召、bool的,請(qǐng)使用如下類型進(jìn)行轉(zhuǎn)換:
float蟹地,int 請(qǐng)使用NSNumber類型
bool請(qǐng)使用BOOL類型积暖。
例如下面代碼,我們傳入2個(gè)浮點(diǎn)數(shù)怪与,然后計(jì)算他們的合并返回夺刑,我們使用NSNumber而不是int、float去作為參數(shù)類型分别。
+(float) addTwoNumber:(NSNumber *)num1 and:(NSNumber *)num2{
float result = [num1 floatValue]+[num2 floatValue];
return result;
}
目前參數(shù)和返回值支持 int, float, bool, string遍愿,其余的類型暫時(shí)不支持。
OC中調(diào)用JS代碼
相關(guān)配置自行谷歌
語句為:
NSInteger coins = [[currentProId stringByReplacingOccurrencesOfString:@"," withString:@""] integerValue];
std::string jsCallStr = cocos2d::StringUtils::format("util.storeLayer.appBuySuccessed(%zd);", coins);
ScriptingCore::getInstance()->evalString(jsCallStr.c_str());
其中util.storeLayer 是一個(gè)全局的layer.js.
appBuySuccessed 是方法,括號(hào)里面是參數(shù).
附j(luò)s調(diào)用oc返回
#define JSO_ERR_OK (0)
#define JSO_ERR_TYPE_NOT_SUPPORT (-1)
#define JSO_ERR_INVALID_AEGUMENTS (-2)
#define JSO_ERR_METHOD_NOT_FOUND (-3)
#define JSO_ERR_EXCEPTION_OCCURRED (-4)
#define JSO_ERR_CLASS_NOT_FOUND (-5)
#define JSO_ERR_VM_FAILURE (-6)