Skip to content

iOS 设备的实时网络状态,且可以监听网络的变化事件

License

Notifications You must be signed in to change notification settings

TWVision/STReachability

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

5 Commits
 
 
 
 
 
 
 
 

Repository files navigation

STReachability

iOS 设备的实时网络状态,且可以监听网络的变化事件

对Reachability的二次封装,接口更简单易用

使用步骤:

  1. 下载并导入STReachability
  2. 在项目中添加SystemConfiguration.framework
  3. 在要使用的控制器中#import "STReachabilityManager.h"即可

常用方法示例

创建STReachabilityManager

    // 创建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];
}

About

iOS 设备的实时网络状态,且可以监听网络的变化事件

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages