【发布时间】:2012-04-12 23:59:46
【问题描述】:
我正在使用Apple's Reachability code 来确定互联网连接是否存在。
现在我发现,当我同时打开蜂窝网络和 WIFI 时,我的蜂窝网络检查显示为 FALSE,而我的 WIFI 检查显示为 TRUE。
我已尝试为此修改 NetworkStatus 返回值。但没有成功。
谁能帮我解决这个问题???
我想要的是当两个网络都打开时,我的可达性应该对两者都显示 TRUE。
谁能帮我理解以下几点:
SCNetworkReachabilityGetFlags(reachabilityRef, &flags)究竟会做什么?-
如何在下面的代码中只检查
networkStatusForFlags??- (NetworkStatus) currentReachabilityStatus { NSAssert(reachabilityRef != NULL, @"currentNetworkStatus called with NULL reachabilityRef"); NetworkStatus retVal = NotReachable; SCNetworkReachabilityFlags flags; if (SCNetworkReachabilityGetFlags(reachabilityRef, &flags)) { if(localWiFiRef) { retVal = [self localWiFiStatusForFlags: flags]; } else { retVal = [self networkStatusForFlags: flags]; } } return retVal; }
以及如何更改以下代码以仅获取NetworkStatusForFlags
- (NetworkStatus) networkStatusForFlags: (SCNetworkReachabilityFlags) flags
{
if ((flags & kSCNetworkReachabilityFlagsReachable) == 0)
{
// if target host is not reachable
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)
{
// ... but WWAN connections are OK if the calling application
// is using the CFNetwork (CFSocketStream?) APIs.
retVal = ReachableViaWWAN;
}
return retVal;
}
编辑:
我使用主机名作为 www.apple.com 并且还尝试了http://www.apple.com。但是在 WIFI 中我得到了 TRUE,只有在蜂窝网络中我得到了 FALSE。
+ (MTPReachability*) reachabilityWithHostName: (NSString*) hostName;
{
MTPReachability* retVal = NULL;
SCNetworkReachabilityRef reachability = SCNetworkReachabilityCreateWithName(NULL, [hostName UTF8String]);
if(reachability!= NULL)
{
retVal= [[[self alloc] init] autorelease];
if(retVal!= NULL)
{
retVal->reachabilityRef = reachability;
retVal->localWiFiRef = NO;
}
}
return retVal;
}
【问题讨论】:
-
我认为这是一个全球性问题。因为当我使用“whatsapp”并且应用程序上的 wifi 和蜂窝数据都突然停止发送消息时,就像没有网络一样!
-
@Malek_Jundi :所以你的意思是没有解决办法???或者有没有其他方法?请看我编辑的问题...
-
请在下面查看我的答案。
标签: iphone objective-c ios ios5 reachability