首页 > 代码库 > 判断网络
判断网络
网络分wifi、3G、2G、无网络连接;
第一种方案:逻辑如下:
1、先判断当前有无网络;
2、有网络的情况下:
(1)、网络为wifi;
(2)、网络为3G;
(3)、网络为2G;
使用reachability里的KShouldPrintReachabilityFlags 就可以详细区分出wifi、3G、2G,重构一下就可以了;
网速 wifi>3G>2G
PS:当然还有一种情况就是网络连接不稳定,wifi、3G的网速有可能比2G慢.
第二种方案:
在服务器设置一个文件,在客户端下载,计算下载时间,不过几K的文件下载时间基本没啥区别,弄几M的文件下载,时间区别很大,但是用户的流量就被浪费了,所以不可取.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 | //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 = @ "没有网络链接" ; } } |
判断网络
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。