【原】cocos2d-js與oc交互(獲取手機電量ios)

本人用的是cocos creator,最近需要獲取手機的電量,cocos2djs代碼與ios代碼交互獲取電量。獲取電量用的是ios代碼的方法。

1、js調(diào)用OC方法。其實吧,cocos2d-JS提供了js直接調(diào)用OC的方法

var ojb = jsb.reflection.callStaticMethod(className, methodNmae, arg1, arg2, .....);

在jsb.reflection.callStaticMethod方法中,我們通過傳入OC的類名,方法名,參數(shù)就可以直接調(diào)用OC的靜態(tài)方法,并且可以獲得OC方法的返回值。

舉個例子:

例子1:

cocos獲取ios設(shè)備的電量:

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,需要用哪個就寫哪個文件,方法寫到對應(yīng)的文件。

注意:最開始我用cc.log('獲取ios電量:'+result);輸出的,試了很多次,在ios端的控制臺都沒輸出,還以為自己代碼報錯了,后來發(fā)現(xiàn)ios控制臺輸出只能使用console.log('獲取ios電量:'+result);,作為cocos2d-js小白的我發(fā)現(xiàn)真相后眼淚都要掉下來。

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傳參數(shù)給ios端,ios端傳給js端參數(shù)或回調(diào)函數(shù))。

例子2:

js端代碼:

1.調(diào)ios端RootViewController文件的+(NSString*)getBatteryNum:(NSString*) cmd withContent:(NSString*) content;方法。

jsb.reflection.callStaticMethod("RootViewController","getBatteryNum:withContent","qwe","erfew");

// console.log('獲取ios電量:'+result);

2.ios端

RootViewController.h

代碼:

#import

@interface RootViewController : UIViewController {

}

//給js端調(diào)用的接口

+(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('返回的參數(shù):'+str);

},

注:關(guān)于oc端傳給cocos2d-js字符串亂碼問題,可以這樣解決:[clientId UTF8String]

例如:用戶進入應(yīng)用,個推生成一個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);

}


對應(yīng)的cocos2d-js代碼:

getTuiSongCId:function(cid){

// cc.sys.localStorage.setItem("pushCId",cid);

console.log("獲取消息推送的cid:"+cid);

},

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容