<span style="font-size:14px;"></span><p style="margin-top: 0px; margin-bottom: 0px; font-size: 18px; line-height: normal; font-family: Menlo; color: rgb(201, 27, 19);"><span style="font-variant-ligatures: no-common-ligatures; color: #822e0e">#import </span><netinet/in.h></p><p style="margin-top: 0px; margin-bottom: 0px; font-size: 18px; line-height: normal; font-family: Menlo; color: rgb(201, 27, 19);"><span style="font-variant-ligatures: no-common-ligatures; color: #822e0e">#import </span><SystemConfiguration/SCNetworkReachability.h></p><p style="margin-top: 0px; margin-bottom: 0px; font-size: 18px; line-height: normal; font-family: Menlo; min-height: 21px;"><span style="color: rgb(195, 34, 117);">typedef</span> <span style="color: rgb(195, 34, 117);">enum</span>{</p><p style="margin-top: 0px; margin-bottom: 0px; font-size: 18px; line-height: normal; font-family: Menlo; min-height: 21px;"> </p><p style="margin-top: 0px; margin-bottom: 0px; font-size: 18px; line-height: normal; font-family: Menlo;"> NetWorkType_WIFI=<span style="font-variant-ligatures: no-common-ligatures; color: #0435ff">3</span>,</p><p style="margin-top: 0px; margin-bottom: 0px; font-size: 18px; line-height: normal; font-family: Menlo;"> NetWorkType_3G,</p><p style="margin-top: 0px; margin-bottom: 0px; font-size: 18px; line-height: normal; font-family: Menlo;"> NetWorkType_2G,</p><p style="margin-top: 0px; margin-bottom: 0px; font-size: 18px; line-height: normal; font-family: Menlo;"> NetWorkType_None,</p><p style="margin-top: 0px; margin-bottom: 0px; font-size: 18px; line-height: normal; font-family: Menlo;">}NetType;</p>
<span style="font-size:14px;">+(NetType)reach{
struct sockaddr_in zeroAddress;
NetType retVal = NetWorkType_None;
bzero(&zeroAddress, sizeof(zeroAddress));
zeroAddress.sin_len = sizeof(zeroAddress);
zeroAddress.sin_family = AF_INET;
SCNetworkReachabilityRef defaultRouteReachability = SCNetworkReachabilityCreateWithAddress(NULL, (struct sockaddr *)&zeroAddress); //创建测试连接的引用:
SCNetworkReachabilityFlags flags;
SCNetworkReachabilityGetFlags(defaultRouteReachability, &flags);
if ((flags & kSCNetworkReachabilityFlagsReachable) == 0)
{
return NetWorkType_None;
}
if ((flags & kSCNetworkReachabilityFlagsConnectionRequired) == 0)
{
retVal = NetWorkType_WIFI;
}
if ((((flags & kSCNetworkReachabilityFlagsConnectionOnDemand ) != 0) ||
(flags & kSCNetworkReachabilityFlagsConnectionOnTraffic) != 0))
{
if ((flags & kSCNetworkReachabilityFlagsInterventionRequired) == 0)
{
retVal = NetWorkType_WIFI;
}
}
if ((flags & kSCNetworkReachabilityFlagsIsWWAN) == kSCNetworkReachabilityFlagsIsWWAN)
{
if((flags & kSCNetworkReachabilityFlagsReachable) == kSCNetworkReachabilityFlagsReachable) {
if ((flags & kSCNetworkReachabilityFlagsTransientConnection) == kSCNetworkReachabilityFlagsTransientConnection) {
retVal = NetWorkType_3G;
if((flags & kSCNetworkReachabilityFlagsConnectionRequired) == kSCNetworkReachabilityFlagsConnectionRequired) {
retVal = NetWorkType_2G;
}
}
}
}
return retVal;
}</span>
<span style="font-size:24px;">获取网络类型</span>