Created
August 28, 2013 15:46
-
-
Save zhangwc/6367560 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//判断当前网络类型 | |
//Reachability.m 中 networkStatusForFlags 方法重构 | |
- (NetworkStatus) networkStatusForFlags: (SCNetworkReachabilityFlags) flags | |
{ | |
if ((flags & kSCNetworkReachabilityFlagsReachable) == 0) | |
{ | |
return NotReachable; | |
} | |
BOOL retVal = NotReachable; | |
if ((flags & kSCNetworkReachabilityFlagsConnectionRequired) == 0) | |
{ | |
// if target host is reachable and no connection is required | |
// then we'll assume (for now) that your on Wi-Fi | |
retVal = ReachableViaWiFi; | |
} | |
if ((((flags & kSCNetworkReachabilityFlagsConnectionOnDemand ) != 0) || | |
(flags & kSCNetworkReachabilityFlagsConnectionOnTraffic) != 0)) | |
{ | |
// ... and the connection is on-demand (or on-traffic) if the | |
// calling application is using the CFSocketStream or higher APIs | |
if ((flags & kSCNetworkReachabilityFlagsInterventionRequired) == 0) | |
{ | |
// ... and no [user] intervention is needed | |
retVal = ReachableViaWiFi; | |
} | |
} | |
if ((flags & kSCNetworkReachabilityFlagsIsWWAN) == kSCNetworkReachabilityFlagsIsWWAN) | |
{ | |
if((flags & kSCNetworkReachabilityFlagsReachable) == kSCNetworkReachabilityFlagsReachable) { | |
if ((flags & kSCNetworkReachabilityFlagsTransientConnection) == kSCNetworkReachabilityFlagsTransientConnection) { | |
retVal = ReachableVia3G; | |
if((flags & kSCNetworkReachabilityFlagsConnectionRequired) == kSCNetworkReachabilityFlagsConnectionRequired) { | |
retVal = ReachableVia2G; | |
} | |
} | |
} | |
} | |
return retVal; | |
} | |
//检查当前网络连接是否正常 | |
-(BOOL)connectedToNetWork | |
{ | |
struct sockaddr_in zeroAddress; | |
bzero(&zeroAddress, sizeof(zeroAddress)); | |
zeroAddress.sin_len = sizeof(zeroAddress); | |
zeroAddress.sin_family = AF_INET; | |
SCNetworkReachabilityRef defaultRouteReachability = SCNetworkReachabilityCreateWithAddress(NULL, (struct sockaddr *)&zeroAddress); | |
SCNetworkReachabilityFlags flags; | |
BOOL didRetrieveFlags = SCNetworkReachabilityGetFlags(defaultRouteReachability, &flags); | |
CFRelease(defaultRouteReachability); | |
if (!didRetrieveFlags) { | |
printf("Error. Count not recover network reachability flags\n"); | |
return NO; | |
} | |
BOOL isReachable = flags & kSCNetworkFlagsReachable; | |
BOOL needsConnection = flags & kSCNetworkFlagsConnectionRequired; | |
return (isReachable && !needsConnection) ? YES : NO; | |
} | |
//检查网络连接类型 | |
-(void)checkNetworktype:(id)sender{ | |
NSString *connectionKind; | |
if ([self connectedToNetWork]) { | |
hostReach = [Reachability reachabilityWithHostName:@"www.google.com"]; | |
switch ([hostReach currentReachabilityStatus]) { | |
case NotReachable: | |
connectionKind = @"没有网络链接"; | |
break; | |
case ReachableViaWiFi: | |
connectionKind = @"当前使用的网络类型是WIFI"; | |
break; | |
case ReachableVia3G: | |
connectionKind = @"当前使用的网络链接类型是WWAN(3G)"; | |
break; | |
case ReachableVia2G: | |
connectionKind = @"当前使用的网络链接类型是WWAN(2G)"; | |
break; | |
default: | |
break; | |
} | |
}else { | |
connectionKind = @"没有网络链接"; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment