使用Reachbility判断当前网络状态

  我们使用程序时,有时会断网,但我们并不知道,程序也没有说明。作为程序员,是肯定要为用户考虑的,废话不多说,上代码。
   Reachability *netTest;//定义一个Reachability
  接着呢,如何判断有网没网,有的时候,在家中,路由器开着,我们手机也连着wifi,我们就会认为连着网,但我们不知道是否可以进行互联网反问,这就需要输入一个网络地址,去进行判断。同样,程序中,我们也需要,有一个IP地址,去判断手机是否能上网。
   NSString *address = @"www.baidu.com";//例子
同时,需要判断你输入的地址是否合法域名,
+(BOOL)judiceIsHost:(NSString *)string{
//正则表达式,判断是否为合法域名
    NSString *test = @"[^//]*?\\.(com|cn|net|org|biz|info|cc|tv)";
//"RegexKitLite.h" 中的方法
    if ([string isMatchedByRegex:test]) {
        return YES;
    }else
        return NO;
}
if([self judiceIsHost:address])
{
netTest= [Reachability reachabilityWithHostName:address];
}
 
//接下来,判断网络状态
        const char *iPAdd = [address UTF8String];
        struct sockaddr_in address;
        memset(&address, 0, sizeof(address));
        address.sin_len = sizeof(address);
        address.sin_family = AF_INET;
        address.sin_port = htonl(port);
        address.sin_addr.s_addr = htons(inet_addr(iPAdd));
       
        netTest = [Reachability reachabilityWithAddress:&address];
  switch ([netTest currentReachabilityStatus])
    {
        case NotReachable:
            NSLog(@"无网络状态");
            self.beforeNetType = NET_NONE;
            self.currentNetType = NET_NONE;
            [SHServer instance].beforeNetType = NET_NONE;
            break;
        case ReachableViaWWAN:
            NSLog(@"WWAN网络状态");
            self.beforeNetType = NET_WWAN;
            self.currentNetType = NET_WWAN;
            [SHServer instance].beforeNetType = NET_WWAN;
            break;
        case ReachableViaWiFi:
            NSLog(@"NetWifi网络状态");
            self.beforeNetType = NET_WIFE;
            self.currentNetType = NET_WIFE;
            [SHServer instance].beforeNetType = NET_WIFE;
            break;
        default:
            break;
    }
 //如果要求监听才开始监听网络 listten/布尔值,/
    if (listen)
    {
        [[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(reachabilityChange:) name:kReachabilityChangedNotification object:nil];
        [netTest startNotifier];
    }
/*网络变化时的回掉方法*/
-(void)reachabilityChange:(NSNotification *)notice
{
    Reachability *newNetTest = [notice object];
    NSLog(@"%d--%d",self.beforeNetType,[SHServer instance].beforeNetType);
    switch ([newNetTest currentReachabilityStatus]) {
        case NotReachable:
            self.currentNetType = NET_NONE;
            NSLog(@"无网络状态1");
            break;
        case ReachableViaWiFi:
            self.currentNetType = NET_WIFE;
            NSLog(@"Wife网络状态");
            break;
        case ReachableViaWWAN:
            self.currentNetType = NET_WWAN;
            NSLog(@"WWAN网络状态");
            break;
           
        default:
            break;
    }
    [newNetTest startNotifier];
   
    if ([SHServer instance].beforeNetType == NET_NONE && self.currentNetType != NET_NONE)
    {
        //如果是从没网到有网
        NSLog(@"没网  到   有网");
        self.beforeNetType = self.currentNetType;
        [SHServer instance].beforeNetType = self.currentNetType;
        [[SHServer instance].netNoticeView hideNetNoticeView];
        [SocketCommunication instance].unNeedReConnect = NO;
        //socket开始重连
        [[SocketCommunication instance] socketReConnect];
       
    }
   
    else if ([SHServer instance].beforeNetType != NET_NONE && self.currentNetType == NET_NONE)
    {
        NSLog(@"有网  到  没网");
       
        self.beforeNetType = self.currentNetType;
        [SHServer instance].beforeNetType = self.currentNetType;
        [SocketCommunication instance].unNeedReConnect = YES;
        [[SHServer instance].netNoticeView showNetNoticeViewWithText:@"无网络连接,请检查网络" andButtonTitle:nil];
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值