當(dāng)我們第一次下載一個全新app的時候,打開app會提示一些授權(quán)彈窗,最主要的就是網(wǎng)絡(luò),有的用戶點擊不允許使用網(wǎng)絡(luò),那app將無法給用戶提供相應(yīng)的數(shù)據(jù)展示,這個時候,可以做一個彈窗提示,提示用戶去設(shè)置里打開網(wǎng)絡(luò)授權(quán)。
我這里需求是用戶在注冊時,點擊獲取驗證碼處給出提示
第一步:在你用到的地方引入頭文件#import <CoreTelephony/CTCellularData.h>
第二步:聲明一個字符串,會來標(biāo)記該app的授權(quán)網(wǎng)絡(luò)狀態(tài)
@property(nonatomic, copy) NSString *netState;
第三步:檢測app授權(quán)網(wǎng)絡(luò)狀態(tài),在viewDidLoad里進(jìn)行調(diào)用
#pragma mark -- 檢測app是否授權(quán)網(wǎng)絡(luò)狀態(tài)
- (void)networkState{
? ? CTCellularData *cellularData = [[CTCellularData alloc]init];
? ? cellularData.cellularDataRestrictionDidUpdateNotifier = ^(CTCellularDataRestrictedState state){
? ? ? ? BOOL_isRestricted =YES;
? ? ? ? //獲取聯(lián)網(wǎng)狀態(tài)
? ? ? ? switch(state) {
? ? ? ? ? ? case kCTCellularDataRestricted:
? ? ? ? ? ? ? ? NSLog(@"Restricted");//拒絕
? ? ? ? ? ? ? ? break;
? ? ? ? ? ? case kCTCellularDataNotRestricted:
? ? ? ? ? ? ? ? _isRestricted =NO;
? ? ? ? ? ? ? ? NSLog(@"Not Restricted");//允許
? ? ? ? ? ? ? ? break;
? ? ? ? ? ? case kCTCellularDataRestrictedStateUnknown:
? ? ? ? ? ? ? ? NSLog(@"Unknown");//未知
? ? ? ? ? ? ? ? break;
? ? ? ? ? ? default:
? ? ? ? ? ? ? ? break;
? ? ? ? };
? ? ? ? if(_isRestricted ==YES) {
? ? ? ? ? ? self.netState=@"需開啟";
? ? ? ? }else{
? ? ? ? ? ? self.netState=@"無需開啟";
? ? ? ? }
? ? };
}
第四步:在獲取驗證碼的按鈕點擊事件處,先進(jìn)行判斷,網(wǎng)絡(luò)狀態(tài),如之前允許了網(wǎng)絡(luò),正常進(jìn)行獲取驗證碼的網(wǎng)絡(luò)請求;如之前拒絕了網(wǎng)絡(luò)授權(quán),則彈窗提示,去設(shè)置里打開網(wǎng)絡(luò)授權(quán)
-(void)buttonClick{
? ? if([ self.netState isEqualToString:@"需開啟"]){
? ? ? ? dispatch_async(dispatch_get_main_queue(), ^{
? ? ? ? ? ? ? UIAlertController*alertVC = [UIAlertControlleralertControllerWithTitle:@"提示"message:@"您未授權(quán)“開本助手”網(wǎng)絡(luò),請去設(shè)置里,開啟網(wǎng)絡(luò)授權(quán)"preferredStyle:UIAlertControllerStyleAlert];
? ? ? ? ? ? ? UIAlertAction *cancel = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) {}];
? ? ? ? ? ? ? UIAlertAction *sure = [UIAlertAction actionWithTitle:@"去設(shè)置" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
跳轉(zhuǎn)設(shè)置? ? ? ? ? ? ? ?
? [[UIApplication sharedApplication] openURL:[NSURL URLWithString:UIApplicationOpenSettingsURLString]];
? ? ? ? }];
? ? ? ? [alertVCaddAction:cancel];
? ? ? ? [alertVCaddAction:sure];
? ? ? ? [self presentViewController:alertVC animated:YES completion:nil];
? ? ? });
? ? ?}else{
? ? ? ? ? //正常的獲取驗證碼的網(wǎng)絡(luò)請求
? ? ?}
}
注:
1、上面的檢測網(wǎng)絡(luò)狀態(tài),應(yīng)該是屬于異步請求,如果你想在檢測網(wǎng)絡(luò)狀態(tài)那里,去做一些UI操作,比如彈窗寫在那里,會報錯,記得寫上主線程里做UI的操作,就可以了
2、跳轉(zhuǎn)設(shè)置,還需做一個配置
Targets -->info? -->URL Types, 加上prefs
