iOS 檢測網(wǎng)絡(luò)連接狀態(tài)

網(wǎng)絡(luò).jpg

網(wǎng)絡(luò)

不論是APP 還是PC 想上網(wǎng)沖浪都離不開網(wǎng)絡(luò)。但是如果沒有網(wǎng)絡(luò)的時候應(yīng)該如何做才能讓用戶有更好的體驗?zāi)兀?br> 看下效果圖:


無網(wǎng)絡(luò).PNG

實現(xiàn)

  • 寫個單例(因為我們每個頁面都要檢測一下)
    這個單例就是我們顯示沒有網(wǎng)絡(luò)的界面。
    比如里面有:無網(wǎng)絡(luò)的圖片 以及刷新的按鈕!
    主要代碼如下:
    創(chuàng)建單例
  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:@"網(wǎng)絡(luò)狀態(tài)待提升,點擊重試" 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;
}

網(wǎng)絡(luò)檢查

  // 輸出對應(yīng)的網(wǎng)絡(luò)狀態(tài)
  - (void)reachabilityStatus
{
    NetworkStatus status = self.reachability.currentReachabilityStatus;
    if (status == self.status) {
        
    }
    
    self.status = status;
    
    switch (status) {
        case NotReachable:
            NSLog(@"沒有聯(lián)網(wǎng)");
            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;
    }
}

界面調(diào)用

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

原理

就是根據(jù)網(wǎng)絡(luò)的狀態(tài)來選擇在傳入單例的view上顯示什么!

  • 有網(wǎng):顯示正常狀態(tài)
  • 無網(wǎng):顯示無網(wǎng)絡(luò)狀態(tài)

附上鏈接 點我傳送,給個start,好人一生平安

有用請給個喜歡。

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容