您的位置:首页 > 理论基础 > 计算机网络

网络:监听网络(使用Reachability框架)

2016-04-19 14:29 447 查看
#import "ViewController.h"
#import "Reachability.h"
@interface ViewController ()
@property (nonatomic, strong) Reachability *bility;
@end

@implementation ViewController

- (void)viewDidLoad {
[super viewDidLoad];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(networkChangeNotification) name:kReachabilityChangedNotification object:nil];
[self.bility startNotifier];
}

- (void)networkChangeNotification {
NSLog(@"网络发生改变");
NetworkStatus status = self.bility.currentReachabilityStatus;

switch (status) {
case kNotReachable:
NSLog(@"没有网络你玩个毛");
break;
case kReachableViaWiFi:
NSLog(@"不用钱的随便玩");
break;
case kReachableViaWWAN:
NSLog(@"土豪请随意");
break;
}
}

- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event {
NetworkStatus status = self.bility.currentReachabilityStatus;

switch (status) {
case kNotReachable:
NSLog(@"没有网络你玩个毛");
break;
case kReachableViaWiFi:
NSLog(@"不用钱的随便玩");
break;
case kReachableViaWWAN:
NSLog(@"土豪请随意");
break;
}
}

- (Reachability *)bility {
if (_bility == nil) {
_bility = [Reachability reachabilityWithHostName:@"baidu.com"];
}
return _bility;
}

- (void)dealloc {
[[NSNotificationCenter defaultCenter]removeObserver:self name:kReachabilityChangedNotification object:nil];
[self.bility stopNotifier];
NSLog(@"dealloc");
}

@end
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: