美文网首页
iOS怎么判断当前网络状态

iOS怎么判断当前网络状态

作者: Apple技术产品粉 | 来源:发表于2017-07-27 12:53 被阅读0次

    最近公司项目需要判断手机当前的网络状态是2G/3G/4G还是Wifi,下面说明一下iOS端如何判断:

    方法主要有两种,第一种利用苹果官方封装好的Reachability库中,做了一下定制化:

    typedefNS_ENUM(NSUInteger,HJNetWorkStatus) 

    {ZYNetWorkStatusNotReachable= 0,ZYNetWorkStatusUnknown= 1,ZYNetWorkStatusWWAN2G= 2,ZYNetWorkStatusWWAN3G= 3,ZYNetWorkStatusWWAN4G= 4,ZYNetWorkStatusWiFi= 5,

    };

    #import"Reachability.h"

    @property(nonatomic,assign)BOOL isAvaliable;// 当前网络是否可用@property(nonatomic,assign)NSInteger status;// 当前网络状态//发送通知[[NSNotificationCenterdefaultCenter]addObserver:selfselector:@selector(netWorkChanged:) name:kRealReachabilityChangedNotification object:nil];

    - (void)netWorkChanged:(NSNotification*)notification{   

     RealReachability *reachability = (RealReachability *)notification.object;  

     ReachabilityStatus status = [reachability currentReachabilityStatus];self.isUserReal =YES;switch(status)

     {

    case  RealStatusNotReachable:

    self.isAvaliable =NO;

    self.status =0;            

    [self   alertMessage:@"No Network"];

    break;

    case RealStatusViaWiFi:

    self.isAvaliable =YES;

    self.status =1;           

     [self alertMessage:@"WIFI"];

    break;

    case RealStatusViaWWAN:

    self.isAvaliable =YES;

    self.status =2;          

      [selfalertMessage:@"2G/3G/4G"];

    break;

    default:

    break;   

     }

    }

    相关文章

      网友评论

          本文标题:iOS怎么判断当前网络状态

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