博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
IOS判断用户的网络类型(2/3/4G、wifi)
阅读量:6589 次
发布时间:2019-06-24

本文共 3597 字,大约阅读时间需要 11 分钟。

直接贴代码吧,ios7之后是获取的较为准确,7以下我拿iphone5测试的是无法区分3g/2g。连iphone4都能升到7.1.4,而且目前主流的设备7以下的系统已经很少了,这个方案尽管不太完美,但影响不会太大

 

#import 
  string GetNetWorkType() {
    string strNetworkType = "";         //创建零地址,0.0.0.0的地址表示查询本机的网络连接状态     struct sockaddr_storage zeroAddress;         bzero(&zeroAddress, sizeof(zeroAddress));     zeroAddress.ss_len = sizeof(zeroAddress);     zeroAddress.ss_family = AF_INET;         // Recover reachability flags     SCNetworkReachabilityRef defaultRouteReachability = SCNetworkReachabilityCreateWithAddress(NULL, (struct sockaddr *)&zeroAddress);     SCNetworkReachabilityFlags flags;         //获得连接的标志     BOOL didRetrieveFlags = SCNetworkReachabilityGetFlags(defaultRouteReachability, &flags);     CFRelease(defaultRouteReachability);         //如果不能获取连接标志,则不能连接网络,直接返回     if (!didRetrieveFlags)     {
        return strNetworkType;     }             if ((flags & kSCNetworkReachabilityFlagsConnectionRequired) == 0)     {
        // if target host is reachable and no connection is required         // then we'll assume (for now) that your on Wi-Fi         strNetworkType = "WIFI";     }         if (         ((flags & kSCNetworkReachabilityFlagsConnectionOnDemand ) != 0) ||         (flags & kSCNetworkReachabilityFlagsConnectionOnTraffic) != 0         )     {
        // ... and the connection is on-demand (or on-traffic) if the         // calling application is using the CFSocketStream or higher APIs         if ((flags & kSCNetworkReachabilityFlagsInterventionRequired) == 0)         {
            // ... and no [user] intervention is needed             strNetworkType = "WIFI";         }     }         if ((flags & kSCNetworkReachabilityFlagsIsWWAN) == kSCNetworkReachabilityFlagsIsWWAN)     {
        if ([[[UIDevice currentDevice] systemVersion] floatValue] >= 7.0)         {
            CTTelephonyNetworkInfo * info = [[CTTelephonyNetworkInfo alloc] init];             NSString *currentRadioAccessTechnology = info.currentRadioAccessTechnology;                         if (currentRadioAccessTechnology)             {
                if ([currentRadioAccessTechnology isEqualToString:CTRadioAccessTechnologyLTE])                 {
                    strNetworkType =  "4G";                 }                 else if ([currentRadioAccessTechnology isEqualToString:CTRadioAccessTechnologyEdge] || [currentRadioAccessTechnology isEqualToString:CTRadioAccessTechnologyGPRS])                 {
                    strNetworkType =  "2G";                 }                 else                 {
                    strNetworkType =  "3G";                 }             }         }         else         {
            if((flags & kSCNetworkReachabilityFlagsReachable) == kSCNetworkReachabilityFlagsReachable)             {
                if ((flags & kSCNetworkReachabilityFlagsTransientConnection) == kSCNetworkReachabilityFlagsTransientConnection)                 {
                    if((flags & kSCNetworkReachabilityFlagsConnectionRequired) == kSCNetworkReachabilityFlagsConnectionRequired)                     {
                        strNetworkType = "2G";                     }                     else                     {
                        strNetworkType = "3G";                     }                 }             }         }     }             if (strNetworkType == "") {
        strNetworkType = "WWAN";     }         NSLog( @"GetNetWorkType() strNetworkType :  %s", strNetworkType.c_str());         return strNetworkType; }

 

参考链接:

转载于:https://www.cnblogs.com/meteoric_cry/p/4628490.html

你可能感兴趣的文章
Git查看、删除、重命名远程分支和tag【转】
查看>>
浅谈IM软件业务知识——非对称加密,RSA算法,数字签名,公钥,私钥
查看>>
Oracle中REGEXP_SUBSTR及其它支持正则表达式的内置函数小结
查看>>
使用finfo_file()函数检测上传图片的类型
查看>>
GO语言下载、安装、配置
查看>>
表设计VIso
查看>>
zoj 3229 有源汇有上下界的最大流模板题
查看>>
进程通信之共享内存
查看>>
C/C++获取当前系统时间
查看>>
xmlWriter 以UTF-8格式写xml问题
查看>>
Office Web Apps资源
查看>>
hdu5032 Always Cook Mushroom
查看>>
Android多线程断点续传下载
查看>>
Fragment的可见再载入的方法(真正的Fragment的OnResume和OnPause)
查看>>
数据结构--百度百科
查看>>
正确计算linux系统内存使用率
查看>>
关于MapReduce单词统计的例子:
查看>>
【php】利用php的构造函数与析构函数编写Mysql数据库查询类 (转)
查看>>
导出DLLRegisterServer接口遇到的问题
查看>>
压缩算法
查看>>