打开APP
userphoto
未登录

开通VIP,畅享免费电子书等14项超值服

开通VIP
IOS使用Reachability实时检测网络连接状况

 //在程序的启动处,开启通知

- (BOOL)application:(UIApplication *)applicationdidFinishLaunchingWithOptions:(NSDictionary *)launchOptions

{

  //.....

 

//开启网络状况的监听

[[NSNotificationCenter defaultCenteraddObserver:self

       selector:@selector(reachabilityChanged:)

    namekReachabilityChangedNotification

   objectnil];

hostReach =[[Reachability reachabilityWithHostName:@"www.google.com"retain];//可以以多种形式初始化

[hostReach startNotifier]; //开始监听,会启动一个run loop

      [self updateInterfaceWithReachabilityhostReach];

  //.....

}


// 连接改变

 

- (void)reachabilityChanged: (NSNotification*)note

{

Reachability*curReach = [note object];

NSParameterAssert([curReach isKindOfClass:[Reachability class]]);

[self updateInterfaceWithReachability:curReach];

}


//处理连接改变后的情况

 

- (void)updateInterfaceWithReachability: (Reachability*)curReach

{

   //对连接改变做出响应的处理动作。

 

      NetworkStatus status=[curReach currentReachabilityStatus];

    

if (status== NotReachable) { //没有连接到网络就弹出提实况

UIAlertView *alert= [[UIAlertView allocinitWithTitle:@"MyApp Name"

                         message:@"NotReachable"

                         delegate:nil

                         cancelButtonTitle:@"YES" otherButtonTitles:nil];

                         [alert show];

                         [alert release];

}


}

本站仅提供存储服务,所有内容均由用户发布,如发现有害或侵权内容,请点击举报
打开APP,阅读全文并永久保存 查看更多类似文章
猜你喜欢
类似文章
【热】打开小程序,算一算2024你的财运
IOS疯狂基础之网络检测
iphone网络编程
iOS开发 webView的学习及js交互
iOS网络监测如何区分2、3、4G?
5步确保你走向成功
Self-introduction
更多类似文章 >>
生活服务
热点新闻
分享 收藏 导长图 关注 下载文章
绑定账号成功
后续可登录账号畅享VIP特权!
如果VIP功能使用有故障,
可点击这里联系客服!

联系客服