将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
将改为工具类