//开启网络状况的监听
- [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(reachabilityChanged:) name:kReachabilityChangedNotification object:nil];
- self.hostReach = [Reachability reachabilityWithHostName:@"www.baidu.com"] ;
- [self.hostReach startNotifier]; //开始监听,会启动一个run loop
- //网络链接改变时会调用的方法
- -(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:@"暂无法访问书城信息" delegate:nil cancelButtonTitle:@"确定" otherButtonTitles:nil];
- [alert show];
- [alert release];
- self.isReachable = NO;
- }
- else
- {
- UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"网络连接信息" message:@"网络连接正常" delegate:nil cancelButtonTitle:@"确定" otherButtonTitles:nil];
- [alert show];
- [alert release];
- self.isReachable = YES;
- }
- }
-
- AppDelegate *appDlg = (AppDelegate *)[[UIApplication sharedApplication] delegate];
- if(appDlg.isReachable)
- {
- NSLog(@"网络已连接");//执行网络正常时的代码
- }
- else
- {
- NSLog(@"网络连接异常");//执行网络异常时的代码
- }
Reachability只能区分出无网络、wifi和wwan(2G&2.5G&3G)类型的网络连接类型,只需重构networkStatusForFlags方法,即可详细区分出2G与3G网络
02 | - (NetworkStatus) networkStatusForFlags: (SCNetworkReachabilityFlags) flags |
04 | if ((flags & kSCNetworkReachabilityFlagsReachable) == 0) |
09 | BOOL retVal = NotReachable; |
11 | if ((flags & kSCNetworkReachabilityFlagsConnectionRequired) == 0) |
15 | retVal = ReachableViaWiFi; |
19 | if ((((flags & kSCNetworkReachabilityFlagsConnectionOnDemand ) != 0) || |
20 | (flags & kSCNetworkReachabilityFlagsConnectionOnTraffic) != 0)) |
24 | if ((flags & kSCNetworkReachabilityFlagsInterventionRequired) == 0) |
27 | retVal = ReachableViaWiFi; |
31 | if ((flags & kSCNetworkReachabilityFlagsIsWWAN) == kSCNetworkReachabilityFlagsIsWWAN) |
33 | if ((flags & kSCNetworkReachabilityFlagsReachable) == kSCNetworkReachabilityFlagsReachable) { |
34 | if ((flags & kSCNetworkReachabilityFlagsTransientConnection) == kSCNetworkReachabilityFlagsTransientConnection) { |
35 | retVal = ReachableVia3G; |
36 | if ((flags & kSCNetworkReachabilityFlagsConnectionRequired) == kSCNetworkReachabilityFlagsConnectionRequired) { |
37 | retVal = ReachableVia2G; |
46 | -( BOOL )connectedToNetWork |
48 | struct sockaddr_in zeroAddress; |
49 | bzero(&zeroAddress, sizeof (zeroAddress)); |
50 | zeroAddress.sin_len = sizeof (zeroAddress); |
51 | zeroAddress.sin_family = AF_INET; |
53 | SCNetworkReachabilityRef defaultRouteReachability = SCNetworkReachabilityCreateWithAddress(NULL, ( struct sockaddr *)&zeroAddress); |
54 | SCNetworkReachabilityFlags flags; |
56 | BOOL didRetrieveFlags = SCNetworkReachabilityGetFlags(defaultRouteReachability, &flags); |
57 | CFRelease(defaultRouteReachability); |
59 | if (!didRetrieveFlags) { |
60 | printf ( "Error. Count not recover network reachability flags\n" ); |
64 | BOOL isReachable = flags & kSCNetworkFlagsReachable; |
65 | BOOL needsConnection = flags & kSCNetworkFlagsConnectionRequired; |
66 | return (isReachable && !needsConnection) ? YES : NO; |
70 | -( void )checkNetworktype:(id)sender{ |
71 | NSString *connectionKind; |
72 | if ([self connectedToNetWork]) { |
73 | hostReach = [Reachability reachabilityWithHostName:@ "www.google.com" ]; |
74 | switch ([hostReach currentReachabilityStatus]) { |
76 | connectionKind = @ "没有网络链接" ; |
78 | case ReachableViaWiFi: |
79 | connectionKind = @ "当前使用的网络类型是WIFI" ; |
82 | connectionKind = @ "当前使用的网络链接类型是WWAN(3G)" ; |
85 | connectionKind = @ "当前使用的网络链接类型是WWAN(2G)" ; |
91 | connectionKind = @ "没有网络链接" ; |