问题描述
在检查iPAD 3 WiFi + Cellular的可达性(网络可用性)时,我遇到了下面提到的场景中发生的奇怪问题。
While checking for reachability(network availability) of an iPAD 3 WiFi+Cellular, I ran into across a weird issue that happened in the below mentioned scenario.
- 为了检查网络可用性,我使用了的示例代码。
- 以下代码用于检查WiFi或WWAN的可用性。
- To check network availability I have used Apple sample code forreachability.
- The following code was implemented to check availability of WiFi or WWAN.
`
- (BOOL)networkCheck
{
Reachability *wifiReach = [Reachability reachabilityForInternetConnection];
NetworkStatus netStatus = [wifiReach currentReachabilityStatus];
switch (netStatus)
{
case NotReachable:
{
NSLog(@"%@",@"NETWORKCHECK: Not Connected");
return false;
break;
}
case ReachableViaWiFi:
{
NSLog(@"%@",@"NETWORKCHECK: Connected Via WiFi");
return true;
break;
}
case ReachableViaWWAN:
{
NSLog(@"%@",@"NETWORKCHECK: Connected Via WWAN");
return true;
break;
}
}
return false;
}
`
- 在iPAD&中没有SIM卡的情况下还有
无WiFi连接,上面的方法执行ReachableViaWWAN案例,
似乎完全不正确因为没有SIM或任何其他
WWAN网络可用。
- In a scenario when there was NO SIM in the iPAD & also there wasNo-WiFi connection, the above method executes ReachableViaWWAN case,which seems totally incorrect as there is NO SIM or any otherWWAN network available.
为了克服这个问题,建议采用一种解决方法(或者我应该说是黑客攻击)。实施如下:
向可靠的主机发送请求&检查其响应。
To Overcome this issue a workaround (or should I say a hack) was suggested & implemented as follows:Send a request to a reliable host & check for its response.
case ReachableViaWWAN:
{
NSLog(@"%@",@"NETWORKCHECK: Connected Via WWAN");
NSData *responseData = [NSURLConnection sendSynchronousRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:@"www.google.com"]] returningResponse:nil error:nil];
if (responseData != nil)
{
return true;
break;
}
else
{
return false;
break;
}
}
我有几个问题:
- 这可能听起来很不寻常,但硬件
或iOS出现问题,即使没有SIM卡,它也是ReachableViaWWAN在
设备中? - 这个问题是否有更好的解决方案(比上面提到的解决方法)?
推荐答案
我不知道你的代码中是否有bug,但你可以尝试使用来检查连接。来自此问题的
。
I don't know if it is a bug in your code but you can try using this to check the connection.from this question.
并检查连接性,您可以创建如下方法:
and for check the connectivity you can create a method like this:
-(BOOL)connected
{
Reachability *reachability = [Reachability reachabilityForInternetConnection];
NetworkStatus networkStatus = [reachability currentReachabilityStatus];
return !(networkStatus == NotReachable);
}
以及您想要检查连接的地方
and wherever you want to check the connection just
if (![self connected]) {
//THERE IS NO WWAN OR WIFI connection
}else{
// connected.
}
它适用于我。
这篇关于响应不正确 - WWAN的可达性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!