#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];
}
};
}