自定义权限修改弹框_(IOS)获取APP蜂窝网权限自定义弹框提示封装

当iOS应用在无网络且非WiFi状态下,通过检查网络状态和蜂窝数据权限,自定义弹窗提示用户开启APP的蜂窝网权限。实现代码包括检测网络状态和显示警告框引导用户至设置中开启权限。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

当app的移动网络被关闭时,而此时在app中使用使用的刚好是手机移动网络时,app是没有网的。

此时需要添加一个提示框,提示用户去开启app的蜂窝网。

解决思路:手机处于手机移动网的情况下,非wifit和无网络状态,来判断app是否有蜂窝网权限。

1.判断是不是于手机移动网  2.判断app是否有蜂窝网权限

//

//  MobileNetworkAccess.h

//  Wallet

//

//  Created by Zhanggaoju on 2018/9/5.

//  Copyright © 2018年 www.picc.com. All rights reserved.

//

#import

@interfaceMobileNetworkAccess :NSObject

+(MobileNetworkAccess*) sharedMobileNetworkAccess;

//app蜂窝网关闭是弹框提示

-(void)mobileNetworkAccessWithViewController:(UIViewController*)viewController;

@end

//

//  MobileNetworkAccess.m

//  Wallet

//

//  Created by Zhanggaoju on 2018/9/5.

//  Copyright © 2018年 www.picc.com. All rights reserved.

//

#import "MobileNetworkAccess.h"

#import "BaseAlertController.h"

@importCoreTelephony;

staticMobileNetworkAccess*__mobileNetworkAccess;

@implementationMobileNetworkAccess

//第一次调用该类时调用

+(void)initialize{

[MobileNetworkAccess sharedMobileNetworkAccess];

}

//获取使用实例

+(MobileNetworkAccess*)sharedMobileNetworkAccess{

static dispatch_once_t oneToken;

dispatch_once(&oneToken, ^{

__mobileNetworkAccess = [[MobileNetworkAccess alloc]init];

});

return __mobileNetworkAccess;

}

+(instancetype)alloc{

//如果已经初始化了

if (__mobileNetworkAccess) {

return __mobileNetworkAccess;

}

return [super alloc];

}

-(void)mobileNetworkAccessWithViewController:(UIViewController*)viewController{

intnetworkingStates = [selfnetworkingStates];

if(networkingStates==1||networkingStates==2||networkingStates==3) {

// 应用启动后,检测应用中是否有联网权限

CTCellularData*cellularData = [[CTCellularDataalloc]init];

cellularData.cellularDataRestrictionDidUpdateNotifier = ^(CTCellularDataRestrictedState state){

switch(state) {

case kCTCellularDataRestricted:

// app网络权限受限

[selfalertWithNetworkAccessibleState:viewController];

break;

case kCTCellularDataRestrictedStateUnknown:

// app网络权限不确定

break;

case kCTCellularDataNotRestricted:

// app网络权限不受限

break;

default:

break;

}

};

}

}

- (int)networkingStates

{

// 状态栏是由当前app控制的,首先获取当前app

UIApplication *app = [UIApplication sharedApplication];

NSArray*children;

//IOS11以后,使用状态栏中图标判断当前网络的具体状态 iPhoneX的状态栏是多嵌套了一层,要多取一次 不能用 [[self deviceVersion] isEqualToString:@"iPhone X"] 来判断,因为模拟器不会返回 iPhone X

if([[app valueForKeyPath:@"_statusBar"]

isKindOfClass:NSClassFromString(@"UIStatusBar_Modern")])

{

children= [[[[appvalueForKeyPath:@"_statusBar"]

valueForKeyPath:@"_statusBar"]

valueForKeyPath:@"foregroundView"]

subviews];

}else{

children= [[[appvalueForKeyPath:@"_statusBar"]

valueForKeyPath:@"foregroundView"]

subviews];

}

inttype =0;

for(idchildinchildren) {

if ([child isKindOfClass:[NSClassFromString(@"UIStatusBarDataNetworkItemView") class]]) {

type = [[childvalueForKeyPath:@"dataNetworkType"]intValue];

}

}

returntype;

}

-(void)alertWithNetworkAccessibleState:(UIViewController*)viewController{

BaseAlertController*alertController = [BaseAlertControlleralertControllerWithTitle:@"已为“app名称”关闭蜂窝移动数据"message:@"您可以在“设置”中为此应用打开蜂窝移动数据。"preferredStyle:UIAlertControllerStyleAlert];

UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:@"设置" style:UIAlertActionStyleDestructive handler:^(UIAlertAction * _Nonnull action) {

NSURL *settingsURL = [NSURL URLWithString:UIApplicationOpenSettingsURLString];

if([[UIApplicationsharedApplication]canOpenURL:settingsURL]) {

[[UIApplicationsharedApplication]openURL:settingsURL];

}

}];

[cancelActionsetValue:[UIColor colorWithHexString:@"#593EF5"] forKey:@"titleTextColor"];

[alertControlleraddAction:cancelAction];

UIAlertAction *sureAction = [UIAlertAction actionWithTitle:@"好" style:UIAlertActionStyleDestructive handler:^(UIAlertAction * _Nonnull action) {

}];

[sureActionsetValue:[UIColor colorWithHexString:@"#593EF5"] forKey:@"titleTextColor"];

[alertControlleraddAction:sureAction];

// 由于它是一个控制器 直接modal出来就好了

[viewControllerpresentViewController:alertControlleranimated:YEScompletion:nil];

}

@end

调用:

[[MobileNetworkAccess sharedMobileNetworkAccess] mobileNetworkAccessWithViewController:self];

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值