iOS Reachability监控网络使用
来源:互联网 发布:开源cms系统 php商城 编辑:程序博客网 时间:2024/05/20 03:46
//联系人:石虎 QQ: 1224614774昵称:嗡嘛呢叭咪哄
第一步: 在AppDelegate.h添加头文件"Reachability.h"
第二步: 导入框架SystemConfiguration.frame
第三步: 下面是代码:
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
//开启网络状况的监听
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(reachabilityChanged:) name:kReachabilityChangedNotification object:nil];
self.hostReach = [Reachability reachabilityWithHostName:@"www.baidu.com"] ;
//开始监听,会启动一个run loop
[self.hostReach startNotifier];
}
-(void)reachabilityChanged:(NSNotification *)note
{
Reachability *currReach = [note object];
NSParameterAssert([currReach isKindOfClass:[Reachability class]]);
//对连接改变做出响应处理动作
NetworkStatus status = [currReach currentReachabilityStatus];
//如果没有连接到网络就弹出提醒实况
self.isReachable = YES;
if(status == NotReachable)
{
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"网络连接异常" message:nil delegate:nil cancelButtonTitle:@"确定" otherButtonTitles:nil];
[alert show];
[alert release];
self.isReachable = NO;
return;
}
if (status==kReachableViaWiFi||status==kReachableViaWWAN) {
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"网络连接信息" message:@"网络连接正常" delegate:nil cancelButtonTitle:@"确定" otherButtonTitles:nil];
// [alert show];
[alert release];
self.isReachable = YES;
}
}
然后在每个页面的viewWillAppear:加上:
-(void)viewWillAppear:(BOOL)animated
{
[super viewWillAppear:YES];
AppDelegate *appDlg = (AppDelegate *)[[UIApplication sharedApplication] delegate];
if(appDlg.isReachable)
{
NSLog(@"网络已连接");//执行网络正常时的代码
}
else
{
NSLog(@"网络连接异常");//执行网络异常时的代码
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"网络连接异常" message:nil delegate:nil cancelButtonTitle:@"确定" otherButtonTitles:nil];
[alert show];
[alert release];
}
}
这样就可以检查到在运行程序时网络突然的中断和连接。
- iOS Reachability监控网络使用
- iOS网络检测Reachability使用
- OC—网络监控 使用Reachability
- IOS网络篇:网络状态的监控Reachability
- [iOS]网络编程专题:Reachability的使用
- Reachability,ios检测网络使用环境
- iOS中使用 Reachability 检测网络
- iOS中使用 Reachability 检测网络
- iOS中使用 Reachability 检测网络
- [iOS]网络编程专题:Reachability的使用
- iOS中使用 Reachability 检测网络
- iOS中使用 Reachability 检测网络
- iOS 使用Reachability检测网络状态
- iOS中使用 Reachability 检测网络
- [iOS]网络编程专题:Reachability的使用
- iOS中使用 Reachability 检测网络
- [iOS]网络编程专题:Reachability的使用
- iOS 网络状态监测。Reachability 使用
- 读懂Dubbo源码必备知识点之三
- #2 定义路由
- XDOJ1156: 等待队列
- leetcode中两道关于中位数的题295和480
- maven中依赖版本冲突如何解决--四种方式(四种原则)
- iOS Reachability监控网络使用
- 公告栏对话框管理(课堂笔记)
- PID校正仿真分析
- windows网络编程之重叠模型(OVERLAPPED I/O)基础知识
- mac os 安装thrift
- 手机如何扫描识别车牌
- MBProgressHUD-Determinated Mode重绘代码.
- uva 272
- Android开发之Canvas及高级使用