IOS使用Reachability实时检测网络连接状况
2016-07-27 17:42
459 查看
IOS使用Reachability实时检测网络连接状况
//在程序的启动处,开启通知
- (BOOL)application:(UIApplication *)applicationdidFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
//.....
//开启网络状况的监听
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(reachabilityChanged:)
name: kReachabilityChangedNotification
object: nil];
hostReach =[[Reachability reachabilityWithHostName:@"www.google.com"] retain];//可以以多种形式初始化
[hostReach startNotifier]; //开始监听,会启动一个run
loop
[self updateInterfaceWithReachability: hostReach];
//.....
}
// 连接改变
- (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 alloc] initWithTitle:@"MyApp
Name"
message:@"NotReachable"
delegate:nil
cancelButtonTitle:@"YES" otherButtonTitles:nil];
[alert show];
[alert release];
}
}
//在程序的启动处,开启通知
- (BOOL)application:(UIApplication *)applicationdidFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
//.....
//开启网络状况的监听
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(reachabilityChanged:)
name: kReachabilityChangedNotification
object: nil];
hostReach =[[Reachability reachabilityWithHostName:@"www.google.com"] retain];//可以以多种形式初始化
[hostReach startNotifier]; //开始监听,会启动一个run
loop
[self updateInterfaceWithReachability: hostReach];
//.....
}
// 连接改变
- (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 alloc] initWithTitle:@"MyApp
Name"
message:@"NotReachable"
delegate:nil
cancelButtonTitle:@"YES" otherButtonTitles:nil];
[alert show];
[alert release];
}
}
相关文章推荐
- 个人信息安全报告发布:有 APP 每分钟调用位置权限 1468 次
- 下载量超过一亿的流行应用被发现含有恶意模块
- 苹果与Siri的七年之痒:“宫斗”戏码不断上演
- APP添加CNZZ统计插件教程 Android版添加phonegap
- Android APP与媒体存储服务的交互
- java实现收藏名言语句台词的app
- 修改Android App样式风格的方法
- Android App数据格式Json解析方法和常见问题
- iOS开发之路--微博“更多”页面
- 微信APP支付Java代码
- 通过Html网页调用本地安卓(android)app程序代码
- 百度地图API提示230 错误app scode码校验失败的解决办法
- js判断移动端是否安装某款app的多种方法
- PHP实现批量生成App各种尺寸Logo
- Could not load file or assembly "App_Licenses.dll"的问题
- Android桌面组件App Widget用法入门教程
- Android天气预报app改进版
- 开源电商app常用标签"hot"之第三方开源LabelView
- Android开发之App widget用法实例分析