网络状态监測之 Reachability的使用
先下载 Reachability开源库地址:
(一)git hub: https://github.com/tonymillion/Reachability
(二)我自己改动的:http://download.csdn.net/detail/u012460084/8765095
使用:
将Reachability.h和.m文件导入project,在须要使用的地方调用,我是在AppDelegate类中使用的,例如以下:
(.h文件)
#import <UIKit/UIKit.h>
#import "Reachability.h"
@interface AppDelegate : UIResponder <UIApplicationDelegate>
@property (strong, nonatomic) UIWindow *window;
@end
(.m文件)
#import "AppDelegate.h"
@interface AppDelegate ()
@end
@implementation AppDelegate
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
Reachability* reach = [Reachability reachabilityWithHostname:@"www.baidu.com"];
NSLog(@"--current status: %@", reach.currentReachabilityString);
//通知监測
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(reachabilityChanged:) name:kReachabilityChangedNotification object:nil];
//block监測
reach.reachableBlock = ^(Reachability * reachability)
{
NSString * netWorkName = [NSString stringWithFormat:@"Baidu Block Says Reachable:%@", reachability.currentReachabilityString];
dispatch_async(dispatch_get_main_queue(), ^{
NSLog(@"(%@)网络可用!",netWorkName);
if (reachability.isReachableViaWiFi) {
NSLog(@"(%@)当前通过wifi连接",netWorkName);
} else {
NSLog(@"(%@)wifi未开启。不能用",netWorkName);
}
if (reachability.isReachableViaWWAN) {
NSLog(@"(%@)当前通过2g or 3g or 4g连接",netWorkName);
} else {
NSLog(@"(%@)2g or 3g or 4g网络未使用",netWorkName);
}
});
};
reach.unreachableBlock = ^(Reachability * reachability)
{
NSString * netWorkName = [NSString stringWithFormat:@"GOOGLE Block Says Reachable(%@)", reachability.currentReachabilityString];
dispatch_async(dispatch_get_main_queue(), ^{
NSLog(@"(%@)网络不可用!",netWorkName);
});
};
[reach startNotifier];
return YES;
}
- (void)reachabilityChanged:(NSNotification*)note {
Reachability * reach = [note object];
if(!reach.isReachable) {
NSLog(@"网络不可用");
}else{
NSLog(@"网络可用");
}
if (reach.isReachableViaWiFi) {
NSLog(@"当前通过wifi连接");
} else {
NSLog(@"wifi未开启,不能用");
}
if (reach.isReachableViaWWAN) {
NSLog(@"当前通过2g or 3g连接");
} else {
NSLog(@"2g or 3g网络未使用");
}
}
- (void)applicationWillResignActive:(UIApplication *)application {
// Sent when the application is about to move from active to inactive state. This can occur for certain types of temporary interruptions (such as an incoming phone
call or SMS message) or when the user quits the application and it begins the transition to the background state.
// Use this method to pause ongoing tasks, disable timers, and throttle down OpenGL ES frame rates. Games should use this method to pause the game.
}
- (void)applicationDidEnterBackground:(UIApplication *)application {
// Use this method to release shared resources, save user data, invalidate timers, and store enough application state information to restore your application to
its current state in case it is terminated later.
// If your application supports background execution, this method is called instead of applicationWillTerminate: when the user quits.
}
- (void)applicationWillEnterForeground:(UIApplication *)application {
// Called as part of the transition from the background to the inactive state; here you can undo many of the changes made on entering the background.
}
- (void)applicationDidBecomeActive:(UIApplication *)application {
// Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh
the user interface.
}
- (void)applicationWillTerminate:(UIApplication *)application {
// Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:.
}
@end
网络状态监測之 Reachability的使用的更多相关文章
- OC - 18.监听iPhone的网络状态
使用系统的方法来监听网络状态 系统的方法是通过通知机制来实现网络状态的监听 实现网络状态监听的步骤 定义Reachability类型的成员变量来保存网络的状态 @property (nonatomic ...
- iOS网络-06-监听Iphone的网络状态
使用系统的方法来监听网络状态 系统的方法是通过通知机制来实现网络状态的监听 实现网络状态监听的步骤 定义Reachability类型的成员变量来保存网络的状态 @property (nonatomic ...
- Android 监控网络状态
public static boolean isNetworkAvailable(Context context) { ConnectivityManager connectivity = (Conn ...
- Android中判断网络连接是否可用及监控网络状态
Android中判断网络连接是否可用及监控网络状态 作者: 字体:[增加 减小] 类型:转载 获取网络信息需要在AndroidManifest.xml文件中加入相应的权限,接下来详细介绍Android ...
- Android网络状态监控
Android 监控网络状态 在Android网络应用程序开发中,经常要判断网络连接是否可用,因此经常有必要监听网络状态的变化.android的网络状态监听可以用BroadcastReceiver来接 ...
- [Swift通天遁地]四、网络和线程-(12)使用ReachabilitySwift实现对网络状态的检测
★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★➤微信公众号:山青咏芝(shanqingyongzhi)➤博客园地址:山青咏芝(https://www.cnblogs. ...
- Android实践--监測网络状态
Android 监測网络状态 我们在使用Android手机时候,一些APP须要网络环境才干执行,所以手机须要可用的网络,无论是2G.3G或者WIFI.甚至有一些比較耗流量的APP仅仅能在WI ...
- NSURLCache、网络监測状态
有时候.对同一个URL请求多次.返回的数据可能一样的: 比方server上的某张图片.不管下载多少次,返回的数据都是一样的.可是这些情况会造成下面问题: 1,用户流量的浪费. 2.程序响应速度不够快 ...
- iOS网络4——Reachability检测网络状态
一.整体介绍 前面已经介绍了网络访问的NSURLSession.NSURLConnection,还有网页加载有关的webview,基本满足通常的网络相关的开发. 其实在网络开发中还有比较常用的就是网络 ...
随机推荐
- 指定等级 Exercise07_01
import java.util.Scanner; /** * @author 冰樱梦 * 时间:2018年下半年 * 题目:指定等级 * */ public class Exercise07_01 ...
- 显示字符 Exercise06_12
/** * @author 冰樱梦 * 时间:2018年下半年 * 题目:显示字符 *1 2 3 4 5 6 7 8 9 : ; < = > ? @ A B C D E F G H I J ...
- 杂谈PID控制算法——最终篇:C语言实现51单片机中的PID算法
真遗憾,第二篇章没能够发表到首页上去.趁热打铁.把最终篇——代码篇给发上来. 代码的设计思想请移步前两篇文章 //pid.h #ifndef __PID__ #define __PID__ /*PID ...
- python可变的参数列表
一般的计算机语言中参数的个数是不能改变的,但是在python中实参的个数是可以改变的.主要是通过形参中的*arg和**arg来实现的,使用可变参数必须遵守下面规则: 1.位置参数必须出现在这*arg参 ...
- Word绘制跨行表格
如图“用户评价电影数目”,我们需要均分第一行,选中这三个个,设置了表个高度0.5cm,但是发现上面的一个比较考上,我们需要找到水平竖直居中,那么双击表格,打开表格工具,有设计和布局,切换到布局就找到了 ...
- oracle 11g 大量废连接占满数据库连接问题处理
问题描述: 数据库不断出现大量无用连接,超过数据库最大连接数,导致新的连接无法建立,访问不通数据库 问题分析: 服务器netstat连接数,大量连接来自办公网连接,不断在增加,通过服务器spid查看数 ...
- 关于接口 RandomAccess
今天看到java.util.Collections这个工具类中的 public static <T> void fill(List<? super T> list, T obj ...
- wireshark----教你怎样抓包
wireshark----教你怎样抓包 wireshark是一款强大的抓包工具,走过路过一定不要错过就是了,当你学习TCP/IP协议的时候,学习使用wireshark抓包正是理论联系实际最好的方法,先 ...
- javascript数组及操作方法
数组及操作方法 数组就是一组数据的集合,javascript中,数组里面的数据可以是不同类型的. 定义数组的方法 //对象的实例创建 var aList = new Array(1,2,3); //直 ...
- Node.js 解析gzip网页(https)
gzip网页指网页头字段Content-Encoding是gzip(GNU zip)内容编码方式.内容编码是指不丢失实体信息的前提下所进行的压缩. Node.js 代码如下: //========== ...