使用Reachability检测网络(施工)

将Reachability转为arc


在模拟器测试很难区分WiFi和3G的网络,也没有真机测试

//  ViewController.m
//  Reachability施工
//
//  Created by gdarkness on 16/2/25.
//  Copyright © 2016年 gdarkness. All rights reserved.
//

#import "ViewController.h"
#import "Reachability.h"
extern NSString const *kNETWORKRCONNECT = @"Connection";
extern NSString const *kNETWORKDISCONNECT = @"DisConnection";

@interface ViewController ()

@property(nonatomic,readwrite)NSMutableDictionary *boservers;
@property(nonatomic,readwrite)Reachability *internetReach;
@property(nonatomic,readwrite)Reachability *wifiReach;
@property(nonatomic,readwrite)BOOL connect;
@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
}
-(void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
    [self startReachability];
}
-(BOOL)startReachability{
    //初始化
    [[NSNotificationCenter defaultCenter]addObserver:self
                                            selector:@selector(reachabilityChanged:)
                                            name:kReachabilityChangedNotification
                                            object:nil];
    //3G
    self.internetReach = [Reachability reachabilityForInternetConnection];
    [self.internetReach startNotifier];
    BOOL res3G = [self updateInterfaceWithReachability:self.internetReach];
    //WiFi
    self.wifiReach = [Reachability reachabilityForLocalWiFi];
    [self.wifiReach startNotifier];
    BOOL resWifi = [self updateInterfaceWithReachability:self.wifiReach];
    self.connect = res3G || resWifi;
    return res3G || resWifi;
}

-(BOOL)updateInterfaceWithReachability:(Reachability *)curReach{
    NetworkStatus status = [curReach currentReachabilityStatus];
    if (status == NotReachable) {
        //断开连接
        NSLog(@"Disconnected");
        return NO;
    }else if (curReach == self.internetReach) {
            NSLog(@"3G");
        }else if(curReach == self.wifiReach){
            NSLog(@"WIFI");
        }return YES;
}
-(void)reachabilityChanged:(NSNotification *)note{
    Reachability *curReach = [note object];
    NSParameterAssert([curReach isKindOfClass:[Reachability class]]);
    BOOL res = [self updateInterfaceWithReachability:curReach];
    //继续和停止
    if (self.connect != res) {
        self.connect =res;
        if (res) {
            //发布新的通知
            NSNotification *notification = [NSNotification notificationWithName:(NSString *)kNETWORKRCONNECT object:nil];
        }else{
            //发布新的通知
            NSNotification *notification = [NSNotification notificationWithName:(NSString *)kNETWORKDISCONNECT object:nil];
        }
    }
}
@end


将改为工具类





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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值