| 关键词: nbsp connectionKind Reachability isConnected ViewController 网络 netstatus hostReach break curReach |
#import "ViewController.h" #import "Reachability.h" #import "Common.h" @interface ViewController () { NetworkStatus netstatus; BOOL isConnected; Reachability * hostReach; } @end @implementation ViewController - (void)viewDidLoad { [super viewDidLoad]; // Do any additional setup after loading the view, typically from a nib. } - (void)didReceiveMemoryWarning { [super didReceiveMemoryWarning]; // Dispose of any resources that can be recreated. } - (IBAction)butonClick:(id)sender { [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(reachabilityChanged:) name:kReachabilityChangedNotification object:nil]; hostReach = [Reachability reachabilityWithHostName:@"www.baidu.com"]; [hostReach startNotifier]; } // 网络监听事件 -(void)reachabilityChanged:(NSNotification *)note { NSString * connectionKind = nil; Reachability * curReach = [note object]; NSParameterAssert([curReach isKindOfClass:[Reachability class]]); netstatus = [curReach currentReachabilityStatus]; switch (netstatus) { case NotReachable: connectionKind = @"当前没有网络链接\n请检查你的网络设置"; NSLog(@"%@",connectionKind); isConnected =NO; break; case ReachableViaWiFi: connectionKind = @"当前使用的网络类型是WIFI"; NSLog(@"%@",connectionKind); isConnected =YES; break; case ReachableViaWWAN: connectionKind = @"您现在使用的是2G/3G网络\n可能会产生流量费用"; NSLog(@"%@",connectionKind); isConnected =YES; break; default: break; } if (isConnected == NO) { [Common showAlert:connectionKind]; } } demo链接 http://download.csdn.net/my |
|
声明:文章版权归原作者所有 部分文章转自互联网 如有侵权请联系
[邮箱地址] 删除
|