使用Reachability监测自己的网络情况。下面是简单的测试。很简单也很实用的一个库。
资源下载:Reachability工程 要的话给2206618839@qq.com发封邮件
使用了CocoaPods导入Reachability库,CocoaPods怎么用:CocoaPods使用
这个简单的测试用到了常用的方法以及这个库的常用用法,监测了三种网络状态,在这三种网络状态下你可以自己处理一些情况。
//
// ViewController.m
// RearchAbility
//
// Created by Wu on 16/3/9.
// Copyright © 2016年 Wu. All rights reserved.
//
#import "ViewController.h"
#import <Reachability.h>
@interface ViewController ()
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
//每次用其中一个
/**
* 简单的同步
*/
// [self syncReachability];
/**
* 异步-通知(个人推荐)
*/
// [self asyncReachNotification];
/**
* 异步-块
*/
[self asyncReachabilityBlock];
}
- (void)syncReachability {
Reachability *reach = [Reachability reachabilityForInternetConnection];
// 通过打开一个主页来判断网络情况
// Reachability *reach = [Reachability reachabilityWithHostName:@"www.apple.com"];
NSLog(@"--->:%d",reach.isReachable);
NetworkStatus status = [reach currentReachabilityStatus];
switch (status) {
case NotReachable:
{
NSLog(@"NO NET:%@ | %@",reach.currentReachabilityString,reach.currentReachabilityFlags);
}
break;
case ReachableViaWiFi:
{
NSLog(@"WIFI:%@ | %@",reach.currentReachabilityString,reach.currentReachabilityFlags);
}
break;
case ReachableViaWWAN:
{
NSLog(@"WWAN:%@ | %@",reach.currentReachabilityString,reach.currentReachabilityFlags);
}
break;
default:
{
NSLog(@"网络错误!");
}
break;
}
}
/***---***---***---***---***---***---***---***---***---***---***/
- (void)asyncReachNotification {
Reachability *reach = [Reachability reachabilityForInternetConnection];
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(reachabilityChanged:)
name:kReachabilityChangedNotification
object:nil];
[reach startNotifier];<span style="font-family: Arial, Helvetica, sans-serif;">//开始监听,会启动一个run loop</span>
}
- (void)reachabilityChanged:(NSNotification *)sender {
NSLog(@"网络变化的时候我就显示哦,么么哒");
//NSNotification有两个方法:name、object。object指向发通知的对象
Reachability *reach = [sender object];
NSString *str;
switch (reach.currentReachabilityStatus) {
case NotReachable:
{
str = reach.currentReachabilityString;
//todo what you want when no net
}
break;
case ReachableViaWWAN:
{
str = reach.currentReachabilityString;
//todo what you want when WWAN
}
break;
case ReachableViaWiFi:
{
str = reach.currentReachabilityString;
//todo what you want when WiFi
}
break;
default:
{
str = @"这是我不知道的东东状态";
}
break;
}
UIAlertView *alert = [[UIAlertView alloc]initWithTitle:@"ReachabilityStatus"
message:str
delegate:nil
cancelButtonTitle:@"OK"
otherButtonTitles:nil];
[alert show];
}
/***---***---***---***---***---***---***---***---***---***---***/
- (void)asyncReachabilityBlock {
Reachability *reach = [Reachability reachabilityForInternetConnection];
reach.reachableBlock = ^(Reachability * reachability){
dispatch_async(dispatch_get_main_queue(), ^{
if (reach.currentReachabilityStatus == ReachableViaWiFi) {
NSLog(@"WiFi");
//todo what you want when WiFi
} else {
NSLog(@"WWAN");
//todo what you want when WWAN
}
});
};
reach.unreachableBlock = ^(Reachability * reachability){
dispatch_async(dispatch_get_main_queue(), ^{
NSLog(@"NO NET");
//todo what you want when no net
});
};
[reach startNotifier];
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
}
@end