先下载 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. Java多线程——AQS框架源码阅读

    AQS,全称AbstractQueuedSynchronizer,是Concurrent包锁的核心,没有AQS就没有Java的Concurrent包.它到底是个什么,我们来看看源码的第一段注解是怎么说 ...

  2. mysql获取分类数量

    1.sql <select id="getTypeNum" resultType="TypeNum" > select count(*) as al ...

  3. [测试技术分享]easyFuzzer使用案例分享

    easyFuzzer使用案例分享 1.简介: easyFuzzer是wooyun的一位白帽子(光刃)提供的一款用于fuzz文件的工具.平时主要是和网络协议安全打交道,和本地软件安全打交道比较少,所以没 ...

  4. Network Connection Lost When Windows 8 Goes To Sleep

    http://www.kapilarya.com/fix-network-connection-lost-when-windows-8-goes-to-sleep http://superuser.c ...

  5. WebService authentication

    http://blog.csdn.net/largestone_187/article/details/5734632 通过SoapHeader对用户口令进行验证,只有授权的用户才可以使用接口.确保了 ...

  6. javascript正则表达式(regular expression)

    一种字符串匹配的模式,用来检查一个串是否含有某种子串.将匹配的子串替换或者从某个串中取出符合某个条件的子串等.注意:在javascript中正则表达式也是一种对象1:创建正则表达式两种方式:隐式创建( ...

  7. lsof/fuser卸载挂载文件

    Linux如何卸载挂载文件   在我们进行远程文件操作的时候,我们经常会出现文件服务出现卸载掉哦情况.例如 umount /mnt/net1umount: /mnt/net1: device is b ...

  8. 最佳eclipse字体推荐(个人觉得)

    首先大家能够看看这里面推荐的最佳十款字体.http://www.iteye.com/news/11102-10-great-programming-font 可是经过測试发现,排名第一的字体在ecli ...

  9. RenderMonkey基本使用方法【转】

    RenderMonkey基本使用方法 楔子: 差不多从年中开始由于工作需要,开始研究Direct3D,这是继大二开始自学DX开始,睽违了6年后再重新学习DX.虽然时间很久了,但是幸亏还是有点基础,所以 ...

  10. jbpm4(参数设置)

    1.processDefinition.getDescription() <process name="task_test_2" xmlns="http://jbp ...