获取 ios 系统网络状况、电量

来源:互联网 发布:瓷砖设计软件手机版 编辑:程序博客网 时间:2024/04/30 10:13

本篇博客出自阿修罗道,转载请注明出处:http://blog.csdn.net/fansongy/article/details/8927734

cocos2d-x中并没有关于获取一些系统底层信息的方法。由于跨平台编译的原因,需要根据平台来自己写。

通常需要显示是网络连接状态和电池的电量这两个

#import "Foundation/Foundation.h"#import "UIKit/UIKit.h"typedef enum{// Apple NetworkStatus Compatible Names.NoNetWork    = 0,ConnWiFi     = 2,Conn3G       = 1} NetState;// 获取电池电量,范围0到1.0。-1表示电量未知。float getBatteryLeve();// 检测WIFI是否可用bool isWIFIEnabled();// 检测3G网络是否可用bool is3GEnabled();//取得网络连接状况NetState getNetState();

实现是用.mm来实现,就是调用一些ios现成的接口。

#include "SystemInfo.h"#import "Reachability.h"NSString* testPage = @"www.baidu.com";float getBatteryLeve(){    [[UIDevice currentDevice] setBatteryMonitoringEnabled:YES];    return [[UIDevice currentDevice] batteryLevel];}// 检测WIFI是否可用bool isWIFIEnabled(){    return ([[Reachability reachabilityForLocalWiFi] currentReachabilityStatus] != NotReachable);}// 检测3G网络是否可用bool is3GEnabled(){    return [[Reachability reachabilityForInternetConnection] isReachableViaWWAN];}NetState getNetState(){        Reachability *r = [Reachability reachabilityWithHostname:testPage];    switch ([r currentReachabilityStatus]) {        case NotReachable:            // 没有网络连接            return NoNetWork;            break;        case ReachableViaWWAN:            // 使用3G网络            return Conn3G;            break;        case ReachableViaWiFi:            // 使用WiFi网络            return ConnWiFi;            break;    }     }

    其中Reachability类是官方给提供的一个测试网络的类。使用时引入Reachability.h 和.m就可以,另外还要在Frameworks中加入SystemConfiguration.framework。一些使用可以百度,我下面的源码中也能找到相应的项目。

 

    源码:http://download.csdn.net/detail/fansongy/5377491

  

原创粉丝点击