iOS获取网络类型的方法汇总
reachability类只能区分wifi和wwan类型,却无法区分2g网和3g网。
网上也有些方法,却都存在bug。
经过网上查找资料和测试,基本上总结了以下几种方法:
1、使用导航栏的方式:(私有api)
代码:
typedef enum {
networktype_none = 0,
networktype_wifi,
networktype_2g,
networktype_3g,
} networktype;
uiapplication *application = [uiapplication sharedapplication]; nsarray *subviews = [[[application valueforkey:@"statusbar"] valueforkey:@"foregroundview"]subviews]; nsnumber *datanetworkitemview = nil; for (id subview in subviews) { if ([subview iskindofclass:[nsclassfromstring(@"uistatusbardatanetworkitemview") class]]) { datanetworkitemview = subview; break; } } networktype networktype = networktype_none; switch ([[datanetworkitemview valueforkey:@"datanetworktype"] integervalue]) { case 0: nslog(@"no wifi or cellular"); networktype = networktype_none; break; case 1: nslog(@"2g"); networktype = networktype_2g; break; case 2: nslog(@"3g"); networktype = networktype_3g; break; default: nslog(@"wifi"); networktype = networktype_wifi; break; } return networktype;
使用这种方法要确保导航栏没有被隐藏,如果导航栏被隐藏则取不到值。另外,该方法存在bug,通过reachability类,在网络类型发生改变时,再执行该代码,获取的网络类型数据没有更新。如果程序进入后台再转到前台,重新执行该代码时,便可以重新获取最新的网络类型数据。这样用户体验不好,当用户在移动中,网络类型发生改变,无法获取最新的网络类型,则页面数据无法更新。(ps:如果哪位大神知道,有什么方法可以刷新数据值,望不吝赐教,感谢!)
2、通过scnetworkreachability类
代码:
struct sockaddr_in zeroaddress; bzero(&zeroaddress, sizeof(zeroaddress)); zeroaddress.sin_len = sizeof(zeroaddress); zeroaddress.sin_family = af_inet; scnetworkreachabilityref defaultroutereachability = scnetworkreachabilitycreatewithaddress(null, (struct sockaddr *)&zeroaddress); //创建测试连接的引用: scnetworkreachabilityflags flags; scnetworkreachabilitygetflags(defaultroutereachability, &flags); if ((flags & kscnetworkreachabilityflagsreachable) == 0) { return networktype_none; } networktype retval = networktype_none; if ((flags & kscnetworkreachabilityflagsconnectionrequired) == 0) { retval = networktype_wifi; } if ((((flags & kscnetworkreachabilityflagsconnectionondemand ) != 0) || (flags & kscnetworkreachabilityflagsconnectionontraffic) != 0)) { if ((flags & kscnetworkreachabilityflagsinterventionrequired) == 0) { retval = networktype_wifi; } } if ((flags & kscnetworkreachabilityflagsiswwan) == kscnetworkreachabilityflagsiswwan) { if((flags & kscnetworkreachabilityflagsreachable) == kscnetworkreachabilityflagsreachable) { if ((flags & kscnetworkreachabilityflagstransientconnection) == kscnetworkreachabilityflagstransientconnection) { retval = networktype_3g; if((flags & kscnetworkreachabilityflagsconnectionrequired) == kscnetworkreachabilityflagsconnectionrequired) { retval = networktype_2g; } } } } return retval;
这种方法跟reachability的方法相同,也无法区分2g网和3g网,但是网上有人测试可以区分,如果有人知道原因,希望指正,谢谢!
3、使用softwareupdateservice.framework(私有api)
准备工作:
导出生成私有api的头文件声明
使用私有或未公开的api,首先需要导出其对应的头文件,在头文件里有相关函数的声明。
工具:
class-dump
class-dump可以从编译后的objective-c的二进制文件中提取对应的数据结构及函数等声明。
使用方法:
为了能在任意目录下使用class-dump命令,首先建议把class-dump文件拷贝到/user/local/bin/目录下,然后就可以在任意目录下执行以下命令:
class-dump /developer/platforms/iphonesimulator.platform/developer/sdks/iphonesimulator3.0.sdk/system/library/frameworks/softwareupdateservices.framework/ >sukit.h
但是bulidsetting 设置framework的search path 时要设置真实的私有库路径,因为我们要用它的可执行文件,只有头文件是不行的。
代码:
nsbundle *b = [nsbundle bundlewithpath:@"/system/library/privateframeworks/softwareupdateservices.framework"]; if ([b load]) { // load class from string sunetworkmonitor *networkmonitor = [sunetworkmonitor sharedinstance]; int nettype = [networkmonitor currentnetworktype]; networktype networktype = networktype_none; switch ( nettype ) { case 0: nslog(@"no wifi or cellular"); networktype = networktype_none; break; case 1: nslog(@"wifi"); networktype = networktype_wifi; break; case 2: nslog(@"2g"); networktype = networktype_2g; break; case 3: nslog(@"3g"); networktype = networktype_3g; break; default: break; }<br> return networktype; } return networktype_none;
这种方法可以获取网络类型,并且在网络类型改变时,获取最新的网络类型。目前还没有发现bug,只是准备阶段的工作比较麻烦。
4、最后一种方法,就是利用ios7系统自带的api获取。只是,ios7以下的系统版本需要上述的方法获取网络类型。
以上所述就是本文的全部内容了,希望大家能够喜欢。
上一篇: 利用iOS动画来模拟音量振动条的实现