iOS 检测网络连接状态

网络.jpg

网络

不论是APP 还是PC 想上网冲浪都离不开网络。但是如果没有网络的时候应该如何做才能让用户有更好的体验呢?
看下效果图:


无网络.PNG

实现

  • 写个单例(因为我们每个页面都要检测一下)
    这个单例就是我们显示没有网络的界面。
    比如里面有:无网络的图片 以及刷新的按钮!
    主要代码如下:
    创建单例
  static ZSCDetectNetWork *instance = nil;
  + (instancetype)sharedInstance
{
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        instance.addView = nil;
        instance.status = 10;
        instance = [[[self class] alloc] init];
        instance.photoImageView = [[UIImageView alloc] init];
        instance.photoImageView.image = [UIImage imageNamed:@"noNetwork"];
        [instance addSubview:instance.photoImageView];
        instance.detectBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        instance.detectBtn.titleLabel.font = [UIFont systemFontOfSize:14];
        [instance.detectBtn setTitle:@"网络状态待提升,点击重试" forState:UIControlStateNormal];
        [instance.detectBtn setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
        [instance.detectBtn addTarget:instance action:@selector(refreshNetwork) forControlEvents:UIControlEventTouchUpInside];
        [instance addSubview:instance.detectBtn];
    });
    return instance;
}
  + (instancetype)allocWithZone:(struct _NSZone *)zone
{
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        instance = [super allocWithZone:zone];
    });
    return instance;
}

网络检查

  // 输出对应的网络状态
  - (void)reachabilityStatus
{
    NetworkStatus status = self.reachability.currentReachabilityStatus;
    if (status == self.status) {
        
    }
    
    self.status = status;
    
    switch (status) {
        case NotReachable:
            NSLog(@"没有联网");
            instance.backgroundColor = [UIColor colorWithRed:239/255.0 green:239/255.0 blue:244/255.0 alpha:1];
            
            instance.frame = instance.addView.frame;
            [instance.addView addSubview:instance];
            
            [instance.photoImageView mas_makeConstraints:^(MASConstraintMaker *make) {
                make.top.equalTo(instance).offset(170);
                make.left.equalTo(instance).offset(instance.frame.size.width/2 - (198/2));
                make.width.mas_equalTo(198);
                make.height.mas_equalTo(151.5);
            }];
            
            [instance.detectBtn mas_makeConstraints:^(MASConstraintMaker *make) {
                make.top.equalTo(instance.photoImageView.mas_bottom).offset(7);
                make.left.equalTo(instance);
                make.width.mas_equalTo(instance.frame.size.width);
                make.height.mas_equalTo(40);
            }];
            
            break;
            
        case ReachableViaWiFi:
            NSLog(@"连接的是WIFI");
            
            [instance removeFromSuperview];
            
            break;
            
        case ReachableVia4G:
            NSLog(@"连接的是4G");
            
            [instance removeFromSuperview];
            break;
            
        case ReachableVia3G:
            NSLog(@"连接的是3G");
            
            [instance removeFromSuperview];
            break;
            
        case ReachableVia2G:
            NSLog(@"连接的是2G");
            
            [instance removeFromSuperview];
            break;
            
        default:
            break;
    }
}

界面调用

ZSCDetectNetWork *network = [ZSCDetectNetWork sharedInstance];
 [network startDetectNetwork:self.view];

原理

就是根据网络的状态来选择在传入单例的view上显示什么!

  • 有网:显示正常状态
  • 无网:显示无网络状态

附上链接 点我传送,给个start,好人一生平安

有用请给个喜欢。

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

  • (一)Masonry介绍 Masonry是一个轻量级的布局框架 拥有自己的描述语法 采用更优雅的链式语法封装自动布...
    木易林1阅读 7,166评论 0 3
  • Masonry是一个轻量级的布局框架,拥有自己的描述语法,采用更优雅的链式语法封装自动布局,简洁明了并具有高可读性...
    3dcc6cf93bb5阅读 5,828评论 0 1
  • 1.网络层:@interface WM_NetworkManager : AFHTTPSessionManager...
    博行天下阅读 2,944评论 0 1
  • 1、禁止手机睡眠[UIApplication sharedApplication].idleTimerDisabl...
    DingGa阅读 4,826评论 1 6
  • 界下科技讯:中国银联发布《中国银行卡产业发展报告(2017)》:移动支付成银行卡产业竞合焦点 中国银联近日发布的《...
    界下科技廖琦玲阅读 1,742评论 0 0

友情链接更多精彩内容