代码语言
.
CSharp
.
JS
Java
Asp.Net
C
MSSQL
PHP
Css
PLSQL
Python
Shell
EBS
ASP
Perl
ObjC
VB.Net
VBS
MYSQL
GO
Delphi
AS
DB2
Domino
Rails
ActionScript
Scala
代码分类
文件
系统
字符串
数据库
网络相关
图形/GUI
多媒体
算法
游戏
Jquery
Extjs
Android
HTML5
菜单
网页交互
WinForm
控件
企业应用
安全与加密
脚本/批处理
开放平台
其它
【
Objectc
】
IOS判断用户的网络类型(2/3/4G、wifi)
作者:
/ 发布于
2016/9/12
/
723
#import <CoreTelephony/CTTelephonyNetworkInfo.h> 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; }
试试其它关键字
同语言下
.
根据当前日期计算星期几
.
屏幕中间的弹框列表
.
禁用iOS的UIView长按默认操作
.
IOS判断用户的网络类型(2/3/4G、wifi)
.
iOS7和iOS6适配,和保留之前的坐标编码习惯,不用刻意
.
iOS开发 iOS8 分割线右移15像素 将其归零
.
ios 常用的宏定义方法
.
IOS应用直接 跳转AppStore 的方法 IOS7以上
.
iOS iOS8中 问题"registerForRemoteNotificationTypes
.
通过当前的view找到所在view的控制器
可能有用的
.
根据当前日期计算星期几
.
屏幕中间的弹框列表
.
禁用iOS的UIView长按默认操作
.
IOS判断用户的网络类型(2/3/4G、wifi)
.
iOS7和iOS6适配,和保留之前的坐标编码习惯,不用刻意
.
iOS开发 iOS8 分割线右移15像素 将其归零
.
ios 常用的宏定义方法
.
IOS应用直接 跳转AppStore 的方法 IOS7以上
.
iOS iOS8中 问题"registerForRemoteNotificationTypes
.
通过当前的view找到所在view的控制器
贡献的其它代码
Label
Copyright © 2004 - 2024 dezai.cn. All Rights Reserved
站长博客
粤ICP备13059550号-3