iphone 判断当前网络状态
2012-07-27 14:42
155 查看
1、添加 framework:
将 SystemConfiguration.framework 添加进工程。
2、下载 http://download.csdn.net/detail/svrsimon/4458137
复制里面的 Reachability.h 和 Reachability.m 到项目中
3、调用的代码:
-(NSString*) GetCurrntNet
{
NSString* result;
Reachability *r = [Reachability reachabilityWithHostName:@"www.apple.com"];
switch ([r currentReachabilityStatus]) {
caseNotReachable: // 没有网络连接
result=nil;
break;
caseReachableViaWWAN: // 使用3G网络
result=@"3g";
break;
caseReachableViaWiFi: // 使用WiFi网络
result=@"wifi";
break;
}
return result;
}
以上代码在虚拟机中的模拟器上测试,有异常,难道测试网络联通不能使用模拟器?
另外,不管是不是插着网线,模拟器的WIFI始终是连着的,不管电脑本身的网络是否断开(同样是在虚拟机中)
+(BOOL) IsEnableWIFI { //始终返回YES
return ([[Reachability reachabilityForLocalWiFi] currentReachabilityStatus] != NotReachable);
}
而3G/GPRS始终是断开的
+ (BOOL) IsEnable3G { //一直返回NO
return ([[Reachability reachabilityForInternetConnection] currentReachabilityStatus] != NotReachable);
}
将 SystemConfiguration.framework 添加进工程。
2、下载 http://download.csdn.net/detail/svrsimon/4458137
复制里面的 Reachability.h 和 Reachability.m 到项目中
3、调用的代码:
-(NSString*) GetCurrntNet
{
NSString* result;
Reachability *r = [Reachability reachabilityWithHostName:@"www.apple.com"];
switch ([r currentReachabilityStatus]) {
caseNotReachable: // 没有网络连接
result=nil;
break;
caseReachableViaWWAN: // 使用3G网络
result=@"3g";
break;
caseReachableViaWiFi: // 使用WiFi网络
result=@"wifi";
break;
}
return result;
}
以上代码在虚拟机中的模拟器上测试,有异常,难道测试网络联通不能使用模拟器?
另外,不管是不是插着网线,模拟器的WIFI始终是连着的,不管电脑本身的网络是否断开(同样是在虚拟机中)
+(BOOL) IsEnableWIFI { //始终返回YES
return ([[Reachability reachabilityForLocalWiFi] currentReachabilityStatus] != NotReachable);
}
而3G/GPRS始终是断开的
+ (BOOL) IsEnable3G { //一直返回NO
return ([[Reachability reachabilityForInternetConnection] currentReachabilityStatus] != NotReachable);
}
相关文章推荐
- iOS-iPhone开发之判断当前的网络状态
- 判断Iphone,Ipad当前网络状态
- 判断Iphone,Ipad当前网络状态
- iPhone开发之判断当前的网络状态
- iphone 判断当前网络状态
- 判断Iphone,Ipad当前网络状态
- iphone 判断当前网络状态
- iPhone判断当前网络连接状态
- iPhone判断当前网络连接状态
- Android检测网络状态,判断当前网络是否可用
- iphone判断当前网络连接类型
- 判断网络状态及当前是什么网络
- iOS 开发判断监测当前网络状态
- IOS中如何判断当前网络状态
- Android 判断当前网络状态从而改变某些控件的状态
- Android中getSystemService介绍,获取系统的运行状态,判断当前网络状态
- Android检测网络状态,判断当前网络是否可用
- iOS中利用CoreTelephony获取用户当前网络状态(判断2G,3G,4G)
- iphone开发使用Reachability判断网络状态
- iPhone开发 判断当前的网络是3g还是wifi