I have an app that uses Apples reachability code. When I tab out of the app, turn on airplane mode, go back into the app, I correctly get a message that says no connection is available. If I go back out turn OFF airplane mode and go back into the app, I STILL get the message that no connection is available. The specific problem code is this:
我有一个应用可以使用苹果的可达性代码。当我退出应用程序时,打开飞机模式,回到应用程序,我正确地得到了一个消息,说没有连接可用。如果我返回关闭飞机模式返回到应用程序,我仍然得到消息没有连接是可用的。具体的问题代码如下:
NetworkStatus status = kNotReachable;
if (SCNetworkReachabilityGetFlags(reachabilityRef, &flags))
{
status = [self networkStatusForFlags: flags];
return status;
}
I get inside the if statement and flags ends up being 0 (kSCNetworkReachabilityFlagsTransientConnection
). What does that mean exactly? Has anyone experienced this and does anyone know a work-around or fix? Been playing with it for hours...
我进入if语句,并且标记为0 (kscnetworkabilityflagsientconnection)。这到底意味着什么呢?有没有人有过这样的经历,有人知道变通的方法吗?和它玩了好几个小时…
4 个解决方案
#1
10
I have found that this is caused by supplying a hostname with a protocol specifier (such as http://hostname
instead of just hostname
). Try specifying just the hostname by itself to see if this fixes your problem.
我发现,这是由于提供了一个带有协议说明符的主机名(比如http://hostname,而不是主机名)。试着自己指定主机名,看看这会不会解决您的问题。
#2
1
After you call SCNetworkReachabilityGetFlags it is important also to call CFRelease to avoid caching the network status. See my implementation below:
调用scnetworkabilitygetflags之后,调用CFRelease来避免缓存网络状态也很重要。看到我的实现如下:
SCNetworkReachabilityRef reachability = SCNetworkReachabilityCreateWithName(NULL, host_name);
可达性和可达性= scnetworkabilitycreatewithname (NULL, host_name);
SCNetworkReachabilityFlags flags;
success = SCNetworkReachabilityGetFlags(reachability, &flags);
bool isAvailable = success && (flags & kSCNetworkFlagsReachable) &&
!(flags & kSCNetworkFlagsConnectionRequired);
CFRelease(reachability);
if (isAvailable) {
NSLog(@"Host is reachable: %d", flags);
return true;
}else{
NSLog(@"Host is unreachable");
return false;
}
#3
0
If the flags were received and they end up being 0, as you've seen, this indicates Airplane Mode is on. However, the results of this check seem to be cached, at least for a short time. Try this: leave your app, turn Airplane Mode off, hit a site in Mobile Safari, then return to your app. This may invalidate the cache.
如果接收到的标志是0,正如您所看到的,这表明飞机模式正在运行。但是,这个检查的结果似乎被缓存了,至少在很短的时间内。试试这个:离开你的应用程序,关闭飞机模式,在移动Safari中点击一个站点,然后返回你的应用程序。这可能会使缓存失效。
#4
0
I had the same problem but it was only happening when i was testing in the simulator. I spent 2 days going crazy and then I tested on the device and it worked as a charm! No idea why...
我也有同样的问题,但只是在我在模拟器中测试的时候才发生。我花了两天时间疯了,然后我在设备上做了测试,效果很好!不知道为什么……
#1
10
I have found that this is caused by supplying a hostname with a protocol specifier (such as http://hostname
instead of just hostname
). Try specifying just the hostname by itself to see if this fixes your problem.
我发现,这是由于提供了一个带有协议说明符的主机名(比如http://hostname,而不是主机名)。试着自己指定主机名,看看这会不会解决您的问题。
#2
1
After you call SCNetworkReachabilityGetFlags it is important also to call CFRelease to avoid caching the network status. See my implementation below:
调用scnetworkabilitygetflags之后,调用CFRelease来避免缓存网络状态也很重要。看到我的实现如下:
SCNetworkReachabilityRef reachability = SCNetworkReachabilityCreateWithName(NULL, host_name);
可达性和可达性= scnetworkabilitycreatewithname (NULL, host_name);
SCNetworkReachabilityFlags flags;
success = SCNetworkReachabilityGetFlags(reachability, &flags);
bool isAvailable = success && (flags & kSCNetworkFlagsReachable) &&
!(flags & kSCNetworkFlagsConnectionRequired);
CFRelease(reachability);
if (isAvailable) {
NSLog(@"Host is reachable: %d", flags);
return true;
}else{
NSLog(@"Host is unreachable");
return false;
}
#3
0
If the flags were received and they end up being 0, as you've seen, this indicates Airplane Mode is on. However, the results of this check seem to be cached, at least for a short time. Try this: leave your app, turn Airplane Mode off, hit a site in Mobile Safari, then return to your app. This may invalidate the cache.
如果接收到的标志是0,正如您所看到的,这表明飞机模式正在运行。但是,这个检查的结果似乎被缓存了,至少在很短的时间内。试试这个:离开你的应用程序,关闭飞机模式,在移动Safari中点击一个站点,然后返回你的应用程序。这可能会使缓存失效。
#4
0
I had the same problem but it was only happening when i was testing in the simulator. I spent 2 days going crazy and then I tested on the device and it worked as a charm! No idea why...
我也有同样的问题,但只是在我在模拟器中测试的时候才发生。我花了两天时间疯了,然后我在设备上做了测试,效果很好!不知道为什么……