如果應(yīng)用中有需要實時監(jiān)聽網(wǎng)絡(luò)連接狀態(tài)需求,AFNetwroking框架里面AFNetworkReachabilityManager可以很方便的實現(xiàn)這樣的需求森渐。
#import <AFNetworking/AFNetworkReachabilityManager.h>//導(dǎo)入框架
- (void)setupNetworkReachabilityManager {
AFNetworkReachabilityManager *manager = [AFNetworkReachabilityManager sharedManager];
[self.manager setReachabilityStatusChangeBlock:^(AFNetworkReachabilityStatus status) {
dispatch_async(dispatch_get_main_queue(), ^{
switch (status) {
case AFNetworkReachabilityStatusUnknown:
NSLog(@"未知網(wǎng)絡(luò)");
break;
case AFNetworkReachabilityStatusNotReachable:
NSLog(@"沒有網(wǎng)絡(luò)");
break;
case AFNetworkReachabilityStatusReachableViaWWAN:
NSLog(@"3G/4G");
break;
case AFNetworkReachabilityStatusReachableViaWiFi:
NSLog(@"wifi");
break;
}
});
}];
[manager startMonitoring]; // 開始監(jiān)聽
}
靈活應(yīng)用。