Xamarin.iOS网络工具类reachability.cs
2014-11-03 16:04
253 查看
reachability.cs是根据原生reachability来进行编写的,是官方网络状态判断的demo。实现原理主要是通过网络地址请求来进行判断,实现了三种状态的监听:网络断开、移动网络(2/3/4G)、WIFI网络.
资源下载:http://download.csdn.net/detail/dengzhaowensky/8115255
示例下载:http://download.csdn.net/detail/dengzhaowensky/8115359
注意:reachability.cs里面的内容尽量不要去动它,由其是hostName,当我们看到www.google.com的时候,总以为它不能访问,所以去修改它,结果是网络条件不同的情况下,无法正常使用。
下面看看,简单的用法:
1
资源下载:http://download.csdn.net/detail/dengzhaowensky/8115255
示例下载:http://download.csdn.net/detail/dengzhaowensky/8115359
注意:reachability.cs里面的内容尽量不要去动它,由其是hostName,当我们看到www.google.com的时候,总以为它不能访问,所以去修改它,结果是网络条件不同的情况下,无法正常使用。
下面看看,简单的用法:
private void CheckNetStatus() { //加载当前网络环境状态 var networkStatus = Reachability.InternetConnectionStatus (); this.Status (networkStatus); //当网络状态发生改变时,会自动回调 Reachability.ReachabilityChanged += (object sender, EventArgs e) => { networkStatus = Reachability.InternetConnectionStatus (); this.Status(networkStatus); }; } /// <summary> /// 网络状态UI展示 /// 【AlertCenter】来源于组件库http://components.xamarin.com/view/alert-center /// </summary> /// <param name="networkStatus">Network status.</param> private void Status(NetworkStatus networkStatus) { switch (networkStatus) { case NetworkStatus.NotReachable: AlertCenter.Default.PostMessage ("网络状态","网络断开,请检查网络是否开启!", UIImage.FromFile ("Icon.png")); this.TipsLable.Text = "网络断开,请检查网络是否开启!"; break; case NetworkStatus.ReachableViaCarrierDataNetwork: AlertCenter.Default.PostMessage ("网络状态","当前使用的是移动网络!", UIImage.FromFile ("Icon.png")); this.TipsLable.Text = "当前使用的是移动网络!"; break; case NetworkStatus.ReachableViaWiFiNetwork: AlertCenter.Default.PostMessage ("网络状态","当前使用的是WIFI网络!", UIImage.FromFile ("Icon.png")); this.TipsLable.Text = "当前使用的是WIFI网络!"; break; } }效果图:
1
相关文章推荐
- Reachability,ios检测网络使用环境
- ios 开发之使用Reachability检测网络状态
- ios-ASIHTTPRequest的Reachability可以监听网络,网络出现状况来不同的处理
- IOS使用Reachability实时检测网络连接状况
- IOS使用Reachability实时检测网络连接状况
- IOS使用Reachability实时检测网络连接状况
- [iOS]网络编程专题:Reachability的使用
- 【iOS开发】---- Reachability 网络监测
- IOS 判断网络连接状态 Reachability
- IOS AFNetworking2.2 setReachabilityStatusChangeBlock检测网络连接状态
- IOS socket 判断网络连接(SCNetworkReachability)
- iOS中使用 Reachability 检测网络
- ios利用Reachability确认网络环境3G/WIFI
- IOS使用Reachability实时检测网络连接状况
- ios 开发之使用Reachability检测网络状态
- ios 网络监测之Reachability
- iOS 网络链接 Reachability的使用
- iOS中使用 Reachability 检测网络
- iOS中使用 Reachability 检测网络
- iOS中使用 Reachability 检测网络