美文网首页IOS知识梳理笔记篇
iOS 检测app是否授权网络状态

iOS 检测app是否授权网络状态

作者: 失忆的程序员 | 来源:发表于2023-04-25 11:15 被阅读0次
    链接网络.png
    #import <CoreTelephony/CTCellularData.h>
    
    
    // MARK: ----- 检测app是否授权网络状态
    
    - (void)networkState
    {
        CTCellularData *cellularData = [[CTCellularData alloc] init];
        cellularData.cellularDataRestrictionDidUpdateNotifier = ^(CTCellularDataRestrictedState state) {
            BOOL _isRestricted = YES;
            // 获取联网状态
            switch(state) {
                case kCTCellularDataRestricted:
                    //NSLog(@"Restricted");// 拒绝
                    break;
                case kCTCellularDataNotRestricted:
                    //NSLog(@"Not Restricted");// 允许
                    _isRestricted = NO;
                    break;
                case kCTCellularDataRestrictedStateUnknown:
                    //NSLog(@"Unknown");// 未知
                    break;
                default:
                    break;
            };
            if (_isRestricted)
            {
                UIAlertController *alertVC = [UIAlertController alertControllerWithTitle:@"提示" message:@"您未授权连接网络,可以在“设置->App->无线数据”中开启“无线数据”,连接网络后才能流畅使用。" preferredStyle:UIAlertControllerStyleAlert];
                //UIAlertAction *cancel = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) {}];
                UIAlertAction *sure = [UIAlertAction actionWithTitle:@"设置" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
                    [[UIApplication sharedApplication] openURL:[NSURL URLWithString:UIApplicationOpenSettingsURLString]];
                }];
                //[alertVC addAction:cancel];
                [alertVC addAction:sure];
                [self presentViewController:alertVC animated:YES completion:nil];
            }
        };
    
    }
    
    链接网络.png

    相关文章

      网友评论

        本文标题:iOS 检测app是否授权网络状态

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