先下载 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的使用的更多相关文章

  1. OC - 18.监听iPhone的网络状态

    使用系统的方法来监听网络状态 系统的方法是通过通知机制来实现网络状态的监听 实现网络状态监听的步骤 定义Reachability类型的成员变量来保存网络的状态 @property (nonatomic ...

  2. iOS网络-06-监听Iphone的网络状态

    使用系统的方法来监听网络状态 系统的方法是通过通知机制来实现网络状态的监听 实现网络状态监听的步骤 定义Reachability类型的成员变量来保存网络的状态 @property (nonatomic ...

  3. Android 监控网络状态

    public static boolean isNetworkAvailable(Context context) { ConnectivityManager connectivity = (Conn ...

  4. Android中判断网络连接是否可用及监控网络状态

    Android中判断网络连接是否可用及监控网络状态 作者: 字体:[增加 减小] 类型:转载 获取网络信息需要在AndroidManifest.xml文件中加入相应的权限,接下来详细介绍Android ...

  5. Android网络状态监控

    Android 监控网络状态 在Android网络应用程序开发中,经常要判断网络连接是否可用,因此经常有必要监听网络状态的变化.android的网络状态监听可以用BroadcastReceiver来接 ...

  6. [Swift通天遁地]四、网络和线程-(12)使用ReachabilitySwift实现对网络状态的检测

    ★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★➤微信公众号:山青咏芝(shanqingyongzhi)➤博客园地址:山青咏芝(https://www.cnblogs. ...

  7. Android实践--监測网络状态

    Android 监測网络状态      我们在使用Android手机时候,一些APP须要网络环境才干执行,所以手机须要可用的网络,无论是2G.3G或者WIFI.甚至有一些比較耗流量的APP仅仅能在WI ...

  8. NSURLCache、网络监測状态

    有时候.对同一个URL请求多次.返回的数据可能一样的: 比方server上的某张图片.不管下载多少次,返回的数据都是一样的.可是这些情况会造成下面问题: 1,用户流量的浪费. 2.程序响应速度不够快 ...

  9. iOS网络4——Reachability检测网络状态

    一.整体介绍 前面已经介绍了网络访问的NSURLSession.NSURLConnection,还有网页加载有关的webview,基本满足通常的网络相关的开发. 其实在网络开发中还有比较常用的就是网络 ...

随机推荐

  1. 1.5(Spring MVC学习笔记) 拦截器(Interceptor)

    一.拦截器 1.1拦截器简介 Spring中的拦截器类似Servlet中的过滤器(Filter),主要用于拦截用户请求, 并进行一定的处理操作(如验证权限.记录日志.设置编码方式等). 1.2拦截器实 ...

  2. Android UI 统一修改Button控件的样式,以及其它系统控件的默认样式

    先介绍下修改原理:首先打开位于android.widget包下面的Button.java文件,这里有一句关键的代码如下: public Button(Context context, Attribut ...

  3. Interaction triggers in WPF

    Interaction Class - static class that owns the Triggers and Behaviors attached properties. Handles p ...

  4. 地图投影与ArcGIS坐标系转换

    1. 通常GIS项目涉及到的坐标系 (1)面向局部区域的大比例尺二维平面:高斯投影(横轴墨卡托) 说明:在市一级的小范围区域的GIS系统,比如规划局.国土局.建设局的系统,大都使用高斯投影,以便与地方 ...

  5. sqlserver用于统计表索引情况

    /*eg: --调用该过程实例 --1 创建临时表 IF OBJECT_ID('tempdb..#index_sql_text') IS NOT NULL DROP TABLE #index_sql_ ...

  6. fedora25安装和docker-ce_清华源

    docker-ce_清华源 https://mirrors.tuna.tsinghua.edu.cn/docker-ce/linux/fedora/ fedora25 docker-ce版本: htt ...

  7. ARP协议具体解释之ARP动态与静态条目的生命周期

    ARP协议详细解释之ARP动态与静态条目的生命周期 ARP动态条目的生命周期 动态条目随时间推移自己主动加入和删除. q  每一个动态ARP缓存条目默认的生命周期是两分钟.当超过两分钟,该条目会被删掉 ...

  8. 数据挖掘算法之关联规则挖掘(一)apriori算法

    关联规则挖掘算法在生活中的应用处处可见,几乎在各个电子商务网站上都可以看到其应用 举个简单的例子 如当当网,在你浏览一本书的时候,可以在页面中看到一些套餐推荐,本书+有关系的书1+有关系的书2+... ...

  9. ngrinder安装

    1.源码编译和部署 官网:http://naver.github.io/ngrinder/ 下载源码后,存在部分依赖库不在maven的远程仓库中,这是可以用下载jar包后,用以下命令打包到本地仓库: ...

  10. win8.1休眠状态下不能进入系统

    win8.1下进入睡眠状态出现的问题: 1.合上盖子或者是点击睡眠状态后唤醒进入锁屏界面.可是仅仅能鼠标移动,键盘全然输入不了,出现假死现象,仅仅能强制重新启动. 2.合上盖子再打开无法唤醒屏幕,必须 ...