美文网首页
【原】cocos2d-js与oc交互(示例获取手机电量ios)

【原】cocos2d-js与oc交互(示例获取手机电量ios)

作者: 紫荆逸 | 来源:发表于2017-07-05 09:15 被阅读339次

    本人用的是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的静态方法,并且可以获得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端的控制台都没输出,还以为自己代码报错了,后来发现ios控制台输出只能使用console.log('获取ios电量:'+result);,作为cocos2d-js小白的我发现真相后眼泪都要掉下来。

    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);
    
    },
    

    相关文章

      网友评论

          本文标题:【原】cocos2d-js与oc交互(示例获取手机电量ios)

          本文链接:https://www.haomeiwen.com/subject/pdvfhxtx.html