iphone开发使用Reachability判断网络状态
在使用Reachability判断网络状态时,我们可以下载苹果官网的Reachability.zip文件,解压之后有一个不错的实例供我们参考。
1、下载 http://developer.apple.com/library/ios/samplecode/Reachability/Reachability.zip
复制里面的 Reachability.h 和 Reachability.m 到项目中
2、添加 framework:
将 SystemConfiguration.framework 添加进工程。
在这里描述一下示例中的代码
下面的代码设置UITextField的值以及显示的图片。
- (void) configureTextField: (UITextField*) textField imageView: (UIImageView*) imageView reachability: (Reachability*) curReach
{NetworkStatus netStatus = [curReach currentReachabilityStatus];BOOL connectionRequired= [curReach connectionRequired];NSString* statusString= @"";switch (netStatus){case NotReachable:{statusString = @"Access Not Available";imageView.image = [UIImage imageNamed: @"stop-32.png"] ;//Minor interface detail- connectionRequired may return yes, even when the host is unreachable. We cover that up here...connectionRequired= NO; break;}case ReachableViaWWAN:{statusString = @"Reachable WWAN";imageView.image = [UIImage imageNamed: @"WWAN5.png"];break;}case ReachableViaWiFi:{statusString= @"Reachable WiFi";imageView.image = [UIImage imageNamed: @"Airport.png"];break;}}if(connectionRequired){statusString= [NSString stringWithFormat: @"%@, Connection Required", statusString];}textField.text= statusString;
}
在这里可以更UITextField的显示,以及图片的显示
- (void) updateInterfaceWithReachability: (Reachability*) curReach
{if(curReach == hostReach){[self configureTextField: remoteHostStatusField imageView: remoteHostIcon reachability: curReach];NetworkStatus netStatus = [curReach currentReachabilityStatus];BOOL connectionRequired= [curReach connectionRequired];summaryLabel.hidden = (netStatus != ReachableViaWWAN);NSString* baseLabel= @"";if(connectionRequired){baseLabel= @"Cellular data network is available.\n Internet traffic will be routed through it after a connection is established.";}else{baseLabel= @"Cellular data network is active.\n Internet traffic will be routed through it.";}summaryLabel.text= baseLabel;}if(curReach == internetReach){ [self configureTextField: internetConnectionStatusField imageView: internetConnectionIcon reachability: curReach];}if(curReach == wifiReach){ [self configureTextField: localWiFiConnectionStatusField imageView: localWiFiConnectionIcon reachability: curReach];}}
当网络状态发生变化时会更改状态,以及UITextField,图片的显示。
- (void) reachabilityChanged: (NSNotification* )note
{Reachability* curReach = [note object];NSParameterAssert([curReach isKindOfClass: [Reachability class]]);[self updateInterfaceWithReachability: curReach];
}
如果我们要实时检测当前的网络状态就要通过注册通知告知当前的网络状态了,在这里就要用到NSNotification
在下面的代码中,注册完通知后调用reachabilityChanged:方法,实现对网络的检测,更新网络显示状态。
- (void) applicationDidFinishLaunching: (UIApplication* )application
{#pragma unused(application)contentView.backgroundColor = [UIColor groupTableViewBackgroundColor];summaryLabel.hidden = YES; // Observe the kNetworkReachabilityChangedNotification. When that notification is posted, the// method "reachabilityChanged" will be called. [[NSNotificationCenter defaultCenter] addObserver: self selector: @selector(reachabilityChanged:) name: kReachabilityChangedNotification object: nil];//Change the host name here to change the server your monitoringremoteHostLabel.text = [NSString stringWithFormat: @"Remote Host: %@", @"www.hopean.com"];hostReach = [[Reachability reachabilityWithHostName: @"www.hopesn.com"] retain];[hostReach startNotifier];[self updateInterfaceWithReachability: hostReach];internetReach = [[Reachability reachabilityForInternetConnection] retain];[internetReach startNotifier];[self updateInterfaceWithReachability: internetReach];wifiReach = [[Reachability reachabilityForLocalWiFi] retain];[wifiReach startNotifier];[self updateInterfaceWithReachability: wifiReach];[window makeKeyAndVisible];}
iphone开发使用Reachability判断网络状态相关推荐
- promise使用promise进行判断网络状态
使用promise是为了达到一个并行异步的目的 // 判断网络状态networkState() {return new Promise((resolve, reject) => {uni.get ...
- Android判断网络状态是否断开
1.Android判断网络状态是否断开 不多说了,看代码吧! /** * 判断网络状态是否可用 * @return true: 网络可用 ; false: 网络不可用 */ public b ...
- android 网络代码是什么意思,Android判断网络状态的代码
本文实例为大家分享了Android判断网络状态的具体代码,供大家参考,具体内容如下 一.权限 需要在AndroidManifest.xml 添加访问权限 二.判断连接状态 /** * 判断是否有网络连 ...
- iOS开发网络篇—Reachability检测网络状态
前言:当应用程序需要访问网络的时候,它首先应该检查设备的网络状态,确认设备的网络环境及连接情况,并针对这些情况提醒用户做出相应的处理.最好能监听设备的网络状态的改变,当设备网络状态连接.断开时,程序也 ...
- Android开发(七)——判断网络状态
项目中难免会出现使用网络的情况,使用网络前得进行网络判断,看网上的网友一般有多种实现版本. 第一种: // 是否有网络连接public static boolean isNetworkConnecte ...
- Android之判断网络状态(网络的连接,改变,和判断2G/3G/4G)
现在app大多都需要从网络上获得数据.所以访问网络是在所难免.但是再访问网络之前,我们应该先做一下网络的状态判断.其实在访问网络之前我们要做一些状态判断,对应一些状态判断来做处理,并不是直接使用Htt ...
- Android 判断网络状态
网络状态如果在5.0之后有了很大的改变,比如添加权限等,如果还是使用之前的判断 返现方法过时了 下面写了2个方法包含5.0之前的写法,和5.0之后的写法 5.0之后需要在配置文件中添加 <use ...
- Android判断网络状态
package com.ch.services; import com.ch.utils.NetWorkUtils; import android.app.Service; import androi ...
- 判断网络状态(移动、WiFi、无网络连接)
首先是用到了一个工具类NetWorkUtil: 工具类代码: import android.content.Context; import android.net.ConnectivityManage ...
最新文章
- 欢迎加入网络管理论坛群
- ul li列表元素浮动导致border没有底边解决办法
- 继承jpanel不显示_房产继承全指南
- Springboot只允许进入登录注册的页面,没登录页面进行拦截。
- 5 结构型模式之 - 适配器模式
- 为什么火狐浏览器中点击按钮失效_各种浏览器审查、监听http头工具介绍
- linux下nmap工具的使用
- echarts 折线上写数值_前端ECharts数据可视化图表案例与介绍
- linux mysql查看所有表_linux下查看指定进程的所有连接信息
- CISA:企业断网3到5天,赶走网络中的 SolarWinds 黑客
- github+hexo搭建博客(一)
- pinterest类网站差异化发展 时光轴成稀饭网突围利器
- 06正交实验法及场景法
- 上帝视角-我是一个线程『转』
- 如何高效率的学习Web前端,个人经验分享
- 【框架】amaze ui学习(一)
- 中秋节到了我给大家用python做一个月饼
- Android蓝牙开发系列文章-蓝牙设备类型知多少?
- 中国大学 MOOC C语言程序设计----编程部分答案解析
- python 判断天干地支年份
热门文章
- Thrift基本原理以及使用介绍
- ffmpeg avformat_open_input返回失败,错误码-10049
- Matlab线条动画
- Docker安装制作
- php的工资详解,php计算税后工资的方法
- 友盟iOS统计中破解和越狱是什么?
- Python-Django毕业设计基于的汉服服装租赁系统(程序+Lw)
- HDU6741秦皇岛CCPC2019MUV LUV UNLIMITED(博弈)
- 用appium一定要注意这个参数,不然就会清空数据了(appium打开软件怎么才能不清空数据)
- 我的网站是用阿里云的独立云虚拟主机,也能配置https加密吗?