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

来源:互联网 发布:worktile for mac 编辑:程序博客网 时间:2024/06/06 00:20
#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
0 0