如何解释 Xamarin.iOS 中的 NetworkReachabilityFlags? [英] How to interpret NetworkReachabilityFlags in Xamarin.iOS?
问题描述
我正在使用 NetworkReachability
来确定我的应用的连接状态:
NetworkReachability(this.currentHostUrl);remoteHostReachability.SetNotification(this.ReachabilityChanged);remoteHostReachability.Schedule(CFRunLoop.Current, CFRunLoop.ModeDefault);
回调方法如下所示:
void ReachabilityChanged(NetworkReachabilityFlags flags){this.reachable = (flags & NetworkReachabilityFlags.Reachable) >0;UIHelpers.GetAppDelegate().UpdateConnectivityToast(this.reachable);}
现在如果我切换到飞行模式,回调会立即被调用并且标志参数为 0.然后,在它再次触发后不久,标志为
ConnectionRequired|IsWWAN|Reachable|TransientConnection
如果我关闭飞行模式,我会得到另一个 0,然后再得到 0
可达
如果我关闭 WiFi 并开启 3G,结果是:
IsWWAN|Reachable|TransientConnection
似乎仅检查 Reachable
是不够的.但这里的逻辑是什么?ConnectionRequired
和 TransientConnection
是什么意思?
引用文档:
<块引用>ConnectionRequired
:可达,但必须先建立连接成立.
TransientConnection
:可以使用瞬态访问主机连接(例如PPP).
但是你可能可以像下面的示例代码那样做:
https://github.com/xamarin/monotouch-样本/blob/master/ReachabilitySample/reachability.cs
它基本上检查 Reachable &&(!ConnectionRequired || IsWWAN)
.
I'm using NetworkReachability
to figure out the connectivity status of my app:
NetworkReachability(this.currentHostUrl);
remoteHostReachability.SetNotification(this.ReachabilityChanged);
remoteHostReachability.Schedule(CFRunLoop.Current, CFRunLoop.ModeDefault);
The callback method looks like this:
void ReachabilityChanged(NetworkReachabilityFlags flags)
{
this.reachable = (flags & NetworkReachabilityFlags.Reachable) > 0;
UIHelpers.GetAppDelegate().UpdateConnectivityToast(this.reachable);
}
Now if I switch to airplane mode, the callback gets called immediately and the flags parameter is 0. Then, shortly after it triggers again and the flags are
ConnectionRequired|IsWWAN|Reachable|TransientConnection
If I turn airplane mode off, I get another 0 and then afterwards
Reachable
If I turn WiFi off and 3G kicks in, the result is:
IsWWAN|Reachable|TransientConnection
It seems like checking for Reachable
alone is not enough. But what's the logic here? What do ConnectionRequired
and TransientConnection
mean?
Quoting the documentations:
ConnectionRequired
: Reachable, but a connection must first be established.
TransientConnection
: The host is reachable using a transient connection (PPP for example).
Xamarin API Doc and iOS Lib Doc
But you probably can do it like the following sample code:
https://github.com/xamarin/monotouch-samples/blob/master/ReachabilitySample/reachability.cs
It basically checks if Reachable && (!ConnectionRequired || IsWWAN)
.
这篇关于如何解释 Xamarin.iOS 中的 NetworkReachabilityFlags?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!