网络状态监测之 Reachability的使用
2015-06-02 18:57
477 查看
先下载 Reachability开源库地址:
(一)git hub: https://github.com/tonymillion/Reachability
(二)我自己修改的:http://download.csdn.net/detail/u012460084/8765095
使用:
将Reachability.h和.m文件导入工程,在需要使用的地方调用,我是在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
(一)git hub: https://github.com/tonymillion/Reachability
(二)我自己修改的:http://download.csdn.net/detail/u012460084/8765095
使用:
将Reachability.h和.m文件导入工程,在需要使用的地方调用,我是在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
相关文章推荐
- android网络相册(带磁盘缓存DiskLruCache 和内存缓存LruCache)
- Java编写网络爬虫笔记(第三部分:Jsoup的强大)
- HttpClient取得自定义的状态码302,并获取Header中的参数Location
- 正确处理浏览器在下载文件时HTTP头的编码问题(Content-Disposition)
- java 从网络Url中下载文件
- AsyncHttpClient 源码分析
- Http通信
- unix网络编程(unp)----源代码环境搭建
- System.ServiceModel.CommunicationException: 接收HTTP 响应时发生错误
- System.ServiceModel.CommunicationException: 接收HTTP 响应时发生错误
- 计算机网络复习总结之网络层(二)
- Foundation--NSHTTPCookieStorage
- 图解HTTPS协议加密解密全过程
- 使用boost做post/get请求开发笔记
- 新家地址 http://vizee.org
- TCP/UDP编程中的问题汇总
- TCP连接建立与关闭
- Android Volley完全解析(二),使用Volley加载网络图片
- Android之Http通信——1.初识Http协议
- UDP协议及包格式