您的位置:首页 > 理论基础 > 计算机网络

iOS学习之--------------使用系统方法检测网络情况

2015-05-28 22:38 686 查看
可以使用系统自带的方法对程序的网络情况进行检测,具体实现代码如下:

@property
(nonatomic,strong)
Reachability * conn

1、创建监听

self.conn = [[NSNotificationCenter

defaultCenter]

addObserver : self

selector
: @selector(check)

name:kReachabilityChangedNotification

object:nil];

self.conn

=[Reachability

reachabilityForInternetConnection];
//开始监控
[self.conn

startNotifier];
- (void)dealloc
{
//结束监控

[self.conn

stopNotifier];
//移除
[[NSNotificationCenter

defaultCenter]removeObserver:self];
}

- (void)check

{

//检测wifi状态

Reachability
*wifi = [Reachability

reachabilityForLocalWiFi];

//检测手机自带的上网功能

Reachability
*conn = [Reachability

reachabilityForInternetConnection];

if
([wifi
currentReachabilityStatus] !=

NotReachable) {

NSLog(@"有wife");

}
else
if
([conn
currentReachabilityStatus]){

NSLog(@"使用手机的3g网络");

}
else
{

NSLog(@"没有网络");
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: