写在AppDelegate 的一个网络检测,通过Reachability的自身状态方法,只要网络状态断开,UIalert就会提示。
网络检测方法,并注册一个通知
- //网络检测****************
- [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(reachabilityChanged:) name: kReachabilityChangedNotification object: nil nil];
- hostReach = [[Reachability reachabilityWithHostName:@"www.baidu.com"] retain];
- [hostReach startNotifier];
当网络状态发生变化时,通知触发的方法。
- - (void)reachabilityChanged:(NSNotification*)note{
- Reachability* curReach= [note object];
- NSParameterAssert([curReach isKindOfClass: [Reachability class]]);
- NetworkStatus status= [curReach currentReachabilityStatus];
- if(status == NotReachable) {
- NSUserDefaults *user = [NSUserDefaults standardUserDefaults];
- [user setBool:1 forKey:@"network"];
- UIAlertView*alert= [[UIAlertView alloc] initWithTitle:@"网络连接被断开"
- message:nil delegate:nil
- cancelButtonTitle:@"确定"otherButtonTitles:nil];
- [alert show];
- [alert release];
- }
- else
- {
- // UIAlertView*alert= [[UIAlertView alloc] initWithTitle:@"网络连接成功"
- // message:nil delegate:nil
- // cancelButtonTitle:@"确定"otherButtonTitles:nil];
- // [alert show];
- // [alert release];
- }