iOS监听网络状态(原生)

  • 打开Xcode
  • command + shift + 0
  • 搜索 'Reachability'
  • open project 拷贝 'Reachablity'到自己项目中
  • 获取当前网络状态
Reachability *reachability = [Reachability reachabilityWithHostName:@"www.baidu.com"];
    NetworkStatus netStatus = [reachability currentReachabilityStatus];
    switch (netStatus) {
        case NotReachable:
            break;
        case ReachableViaWiFi:
            networkStatus = 
            break;
        case ReachableViaWWAN:
            break;
            
        default:
            break;
    }```

- 监听网络状态

[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(reachabilityChanged:) name:kReachabilityChangedNotification object:nil];
[self.hostReachability startNotifier];


- 网络改变获取网络状态

  • (void) reachabilityChanged:(NSNotification )note
    {
    Reachability
    curReach = [note object];
    NSParameterAssert([curReach isKindOfClass:[Reachability class]]);
    NetworkStatus netStatus = [reachability currentReachabilityStatus];
    }```

你可能感兴趣的:(iOS监听网络状态(原生))