使用Reachability网络监测

使用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


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值