How to interpret NetworkReachabilityFlags in Xamarin.iOS?

只愿长相守 提交于 2019-12-11 07:10:08

问题


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?


回答1:


If there is ConnectionRequired present, then there is actually no connectivity, even though Reachable is present so it's something like

bool connectionAvailable = (flags.HasFlag(Reachable) && !flags.HasFlags(ConnectionRequired))



回答2:


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).



来源:https://stackoverflow.com/questions/22938310/how-to-interpret-networkreachabilityflags-in-xamarin-ios

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!