美文网首页
8-3检测网络

8-3检测网络

作者: Zd_silent | 来源:发表于2016-08-03 17:23 被阅读27次

    8-3 我不愿意成为你退而求其次的人

    Reachability

    1.下载,添加Reachability类
    2.为项目添加SystemConfiguration.Framework
    
    Reachability* reach = [Reachability reachabilityWithHostname:@"www.google.com"];
    
    // Tell the reachability that we DON'T want to be reachable on 3G/EDGE/CDMA
    reach.reachableOnWWAN = NO;
    
    [[NSNotificationCenter defaultCenter] addObserver:self
                                             selector:@selector(reachabilityChanged:)
                                                 name:kReachabilityChangedNotification
                                               object:nil];
    
    [reach startNotifier];
    
    
    - (void)reachabilityChanged:(NSNotification *)note
    {
        Reachability* curReach = [note object];
        
        NSParameterAssert([curReach isKindOfClass:[Reachability class]]);
        NetworkStatus netStatus = [curReach currentReachabilityStatus];
        
        switch (netStatus)
        {
            case NotReachable:      // 无网络
            {
                break;
            }
            case ReachableViaWWAN:  // 通过2G/3G/4G连接
            case ReachableViaWiFi:  // 通过Wifi连接
            {
                //网络变化时要注意验证用户身份
                [self authentication];
                
                break;
            }
        }
    }
    

    AFNetWorking

     1.下载安装AFNetWorking
    
    //  网络连接单例
    AFNetworkReachabilityManager *reachabilityManager = [AFNetworkReachabilityManager sharedManager];
    //  打开检测
    [reachabilityManager startMonitoring];
    
    // 检测网络连接的代码块回调
    [reachabilityManager setReachabilityStatusChangeBlock:^(AFNetworkReachabilityStatus status) {
       if (status == AFNetworkReachabilityStatusNotReachable) {
           //网络无连接的提示
       }
    }];
    
    // 网络连接状态
    typedef NS_ENUM(NSInteger, AFNetworkReachabilityStatus) {
       AFNetworkReachabilityStatusUnknown          = -1, //未知
       AFNetworkReachabilityStatusNotReachable     = 0,  //无连接
       AFNetworkReachabilityStatusReachableViaWWAN = 1,  //3G
       AFNetworkReachabilityStatusReachableViaWiFi = 2,  //WIFI
    };
    
    

    YYKit

    待定~

    相关文章

      网友评论

          本文标题:8-3检测网络

          本文链接:https://www.haomeiwen.com/subject/wwyqsttx.html