即使該域不存在,在NetworkReachability上調用'GetFlags'始終會返回'Reachable'。我將附上完整的測試解決方案,但以下 代碼應導致「失敗...」,但會導致「成功=真」。Xamarin NetworkReachability錯誤報告可達性
該代碼基於Reachability示例。
public override void ViewDidLoad()
{
base.ViewDidLoad();
// Perform any additional setup after loading the view, typically from a nib.
string host = "http://nonsense.test12345679.com";
using (var r = new SystemConfiguration.NetworkReachability (host)) {
NetworkReachabilityFlags flags;
if (r.TryGetFlags (out flags)) {
_label.Text = "Success = " + IsReachableWithoutRequiringConnection (flags);
} else {
_label.Text = "Failed to get flags ";
}
}
}
public bool IsReachableWithoutRequiringConnection(NetworkReachabilityFlags flags)
{
// Is it reachable with the current network configuration?
bool isReachable = (flags & NetworkReachabilityFlags.Reachable) != 0;
// Do we need a connection to reach it?
bool noConnectionRequired = (flags & NetworkReachabilityFlags.ConnectionRequired) == 0
|| (flags & NetworkReachabilityFlags.IsWWAN) != 0;
return isReachable && noConnectionRequired;
}
如果切換設備進入飛行模式然後可達是正確 返回假。
我正在測試iPad Air,iOS 9.0.2和最新的穩定Xamarin。
看起來它使用相同的可達性類在連接類中的一些額外的插座測試https://開頭的github .COM/jamesmontemagno/Xamarin.Plugins /樹/主/連接/連接/ Connectivity.Plugin.iOS。你可以在沒有Xamarin.Forms的情況下使用插件嗎? –
是的。一些插件專門用於Xamarin.Forms和其他可用於任何Xamarin應用程序。 – valdetero