iOS - 监测网络状态

第一种

  • 在控制器初始化的时候,检测是否可以打开百度网页,如果可以打开则data不为空,否则为nil,不需要框架简单暴力.
    NSURL *scriptUrl = [NSURL URLWithString:@"https://www.baidu.com"];
    NSData *data = [NSData dataWithContentsOfURL:scriptUrl];
    if (data)
        NSLog(@"Device is connected to the Internet");
    else
        NSLog(@"Device is not connected to the Internet");

第二种

  • 通过网络检测框架Reachability来检测
  • 只需要下载导入.h与.m文件即可
  • 在需要检测的地方创建方法或者直接写到公共类里面写成类方法也可以.
- (BOOL)connected
{
    Reachability *reachability = [Reachability reachabilityForInternetConnection];
    NetworkStatus networkStatus = [reachability currentReachabilityStatus];
    return networkStatus != NotReachable;
}
  • 在需要检测的地方调用
    if (![self connected]) {
        // Not connected
        NSLog(@"没连接");
    } else {
        // Connected. Do some Internet stuff
        NSLog(@"有联网");
    }

第三种

  • 相对前面两种比较好的地方就是不需要手动调用检测网络的方法,直接去实时监听网络状态.
  • 所需要的网络框架是tonymillion/Reachability
  • git地址 https://github.com/tonymillion/Reachability
  • 可以在rootViewController中的-(void) viewWillAppear:(BOOL)animated 中创建监听.
-(void) viewWillAppear:(BOOL)animated
{
    // check for internet connection
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(checkNetworkStatus:) name:kReachabilityChangedNotification object:nil];

    internetReachableFoo = [Reachability reachabilityForInternetConnection];
    [internetReachableFoo startNotifier];

    // check if a pathway to a random host exists
    hostReachable = [Reachability reachabilityWithHostName:@"www.apple.com"];
    [hostReachable startNotifier];
    // now patiently wait for the notification
}
  • 完成通知方法,可以检测目前的网络是什么状态,是否有网络,wifi,以及wwan
-(void) checkNetworkStatus:(NSNotification *)notice
{
    // called after network status changes
    NetworkStatus internetStatus = [internetReachableFoo currentReachabilityStatus];
    switch (internetStatus)
    {
        case NotReachable:
        {
            NSLog(@"The internet is down.");

            break;
        }
        case ReachableViaWiFi:
        {
            NSLog(@"The internet is working via WIFI.");

            break;
        }
        case ReachableViaWWAN:
        {
            NSLog(@"The internet is working via WWAN.");

            break;
        }
    }

    NetworkStatus hostStatus = [hostReachable currentReachabilityStatus];
    switch (hostStatus)
    {
        case NotReachable:
        {
            NSLog(@"A gateway to the host server is down.");
            break;
        }
        case ReachableViaWiFi:
        {
            NSLog(@"A gateway to the host server is working via WIFI.");
            break;
        }
        case ReachableViaWWAN:
        {
            NSLog(@"A gateway to the host server is working via WWAN.");
            break;
        }
    }
}
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值