(IOS)获取APP蜂窝网权限自定义弹框提示封装
当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];