本人用的是cocos creator,最近需要獲取手機的電量,cocos2djs代碼與ios代碼交互獲取電量。獲取電量用的是ios代碼的方法。 里面包含cocos2d-js調用ios類中的方法、ios類中調用cocos2d-js文件里的方法。
一、js調用OC方法。
cocos2d-JS提供了js直接調用OC的方法。
var ojb = jsb.reflection.callStaticMethod(className, methodNmae, arg1, arg2, .....);
在jsb.reflection.callStaticMethod方法中,我們通過傳入OC的類名,方法名,參數就可以直接調用OC的靜態(tài)方法,并且可以獲得OC方法的返回值。
舉個例子:
例子1:
cocos獲取ios設備的電量:
cocos2d-js代碼:
//獲取電量
if (cc.sys.os === cc.sys.OS_IOS) {
var result = jsb.reflection.callStaticMethod("RootViewController","getBatteryNum");
// console.log('獲取ios電量:'+result);
if(result !=undefined && result !=null &&result>=0){
this._batteryNumLabel.active=true;
this._batteryNumLabel.string="您還剩余電量:"+Number(result*100)+"%";
}
}
核心代碼:
var result = jsb.reflection.callStaticMethod("RootViewController","getBatteryNum");
// console.log('獲取ios電量:'+result);
RootViewController這個是ios端的文件,自己決定用哪個,可以使用ViewController也可以是用appdelete,需要用哪個就寫哪個文件,方法寫到對應的文件。
注意:最開始我用cc.log('獲取ios電量:'+result);輸出的,試了很多次,在ios端的控制臺都沒輸出,還以為自己代碼報錯了,后來發(fā)現ios控制臺輸出只能使用console.log('獲取ios電量:'+result);,作為cocos2d-js小白的我發(fā)現真相后眼淚都要掉下來。
ios端代碼:
RootViewController.h
#import
@interface RootViewController : UIViewController {
}
+(NSString*)getBatteryNum;
- (BOOL)prefersStatusBarHidden;
@end
RootViewController.m文件的方法:
+(NSString*)getBatteryNum
{
UIDevice *device = [UIDevice currentDevice];
device.batteryMonitoringEnabled = YES;
return [NSString stringWithFormat:@"%.2f", device.batteryLevel];
}
。
ios傳值給cocos2d-js有兩種方法,
一種就是上面那種用返回值就可獲取到值。
另一種可以使用方法。
下面舉例。(js傳參數給ios端,ios端傳給js端參數或回調函數)。
例子2:
js端代碼:
1.調ios端RootViewController文件的+(NSString)getBatteryNum:(NSString) cmd withContent:(NSString*) content;方法。
jsb.reflection.callStaticMethod("RootViewController","getBatteryNum:withContent","qwe","erfew");
// console.log('獲取ios電量:'+result);
二.oc調用js方法。
ios端
RootViewController.h
代碼:
#import
@interface RootViewController : UIViewController {
}
//給js端調用的接口
+(void)getBatteryNum:(NSString*) cmd withContent:(NSString*) content;
@end
RootViewController.m
代碼:
#include "ScriptingCore.h"
+(void)getBatteryNum:(NSString*) cmd withContent:(NSString*) content{
UIDevice *device = [UIDevice currentDevice];
device.batteryMonitoringEnabled = YES;
if (device.batteryState == UIDeviceBatteryStateUnknown) {
NSLog(@"UnKnow");
}else if (device.batteryState == UIDeviceBatteryStateUnplugged){
NSLog(@"Unplugged");
}else if (device.batteryState == UIDeviceBatteryStateCharging){
NSLog(@"Charging");
}else if (device.batteryState == UIDeviceBatteryStateFull){
NSLog(@"Full");
}
NSLog(@"%f",device.batteryLevel);
char tmp[255]= {0};
sprintf(tmp, "cc.vv.anysdkMgr.dianliang('%.2f')",device.batteryLevel);
ScriptingCore::getInstance()->evalString(tmp);
}
3.js端:
在cc.vv.anysdkMgr的文件里寫上(cc.vv.anysdkMgr這個不固定,具體看自己想寫在哪個文件里)
dianliang:function(str){
console.log('返回的參數:'+str);
},
注:關于oc端傳給cocos2d-js字符串亂碼問題,可以這樣解決:[clientId UTF8String]
例如:用戶進入應用,個推生成一個clientId,用戶想把這個clientId傳給cocos2d-js。這時可以這樣:
OC部分代碼:
#include "ScriptingCore.h"
/** SDK啟動成功返回cid */
- (void)GeTuiSdkDidRegisterClient:(NSString *)clientId {
// [ GTSdk ]:個推SDK已注冊,返回clientId
NSLog(@">>[GTSdk RegisterClient]:%@", clientId);
char tmp[255]= {0};
sprintf(tmp, "cc.vv.anysdkMgr.getTuiSongCId('%s')",[clientId UTF8String]);
ScriptingCore::getInstance()->evalString(tmp);
}
對應的cocos2d-js代碼:
getTuiSongCId:function(cid){
// cc.sys.localStorage.setItem("pushCId",cid);
console.log("獲取消息推送的cid:"+cid);
},