iOS 设备的实时网络状态,且可以监听网络的变化事件
对Reachability的二次封装,接口更简单易用
- 下载并导入STReachability
- 在项目中添加
SystemConfiguration.framework
库 - 在要使用的控制器中
#import "STReachabilityManager.h"
即可
// 创建STReachabilityManager
_reachabilityManager = [STReachabilityManager sharedManager];
// 启动监听
[_reachabilityManager startNotifier];
STNetworkStatus networkStatus = [STReachabilityManager sharedManager].networkStatus;
switch (networkStatus) {
case STNotReachable://无网络
break;
case STReachableViaWiFi://WIFI
break;
case STReachableViaWWAN://网络运营商
break;
default:
break;
}
- (void)viewDidLoad
{
[super viewDidLoad];
// 监听通知
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(reachabilityChanged:) name:STReachabilityManagerNetworkStatusChangedNotification object:nil];
}
- (void)reachabilityChanged:(NSNotification *)note
{
STNetworkStatus networkStatus = [STReachabilityManager sharedManager].networkStatus;
switch (networkStatus) {
case STNotReachable://无网络
break;
case STReachableViaWiFi://WIFI
break;
case STReachableViaWWAN://网络运营商
break;
default:
break;
}
}
记住通知要销毁
- (void)dealloc
{
[[NSNotificationCenter defaultCenter] removeObserver:self name:STReachabilityManagerNetworkStatusChangedNotification object:nil];
}