Support_PowerCheck.h文件
#import <Foundation/Foundation.h>
#import <CoreLocation/CoreLocation.h>//定位
/**
权限授权状态
- PowerStateAllow: 允许
- PowerStateRefuse: 不允许
- PowerStateUndefined: 未申请,未定义
*/
typedef NS_ENUM(NSInteger,PowerState) {
PowerStateAllow = 1,
PowerStateRefuse = 2,
PowerStateUndefined = 3
};
NS_ASSUME_NONNULL_BEGIN
/**
权限检查校验
*/
@interface Support_PowerCheck : NSObject
@property (nonatomic, strong) CLLocationManager *locationManager; //定位信息要使用成员变量否则会被arc提前释放一闪而过
+(instancetype)shareCheckPower;
/// 推送 定位 相册 相机 通讯录 权限
-(NSInteger)checkIsHavepower_All;
/**
依次检查推送和定位权限
*/
-(NSInteger)checkIsHavePower_Push_Location;
/**
定位权限
*/
-(NSInteger )checkIsHavePower_Location;
/**
推送权限
*/
-(NSInteger)checkIsHavePower_PushNotification;
/**
相册使用权限 AllowBlock 如果是为处理选择状态,这个就是选择之后的回调
@param AllowBlock 如果是为处理选择状态,这个就是选择之后的回调
*/
-(NSInteger)checkIsHavePower_Photo:(void (^)(bool IsAllow))AllowBlock;
/**
相机 AllowBlock 如果是为处理选择状态,这个就是选择之后的回调
@param AllowBlock 如果是为处理选择状态,这个就是选择之后的回调
*/
-(NSInteger)checkIsHavePower_Camera:(void (^)(bool IsAllow))AllowBlock;
/**
通信录 使用权限 AllowBlock 如果是为处理选择状态,这个就是选择之后的回调
@param AllowBlock 如果是为处理选择状态,这个就是选择之后的回调
*/
-(NSInteger)checkIsHavePower_Contacts:(void (^)(bool IsAllow))AllowBlock;
@end
NS_ASSUME_NONNULL_END
Support_PowerCheck.m文件
#import "Support_PowerCheck.h"
#import <ContactsUI/ContactsUI.h>//联系人
#import <AVFoundation/AVFoundation.h>//相机
#import <Photos/PHPhotoLibrary.h>//相册
//推送
#ifdef NSFoundationVersionNumber_iOS_9_x_Max
#import <UserNotifications/UserNotifications.h>
#endif
@implementation Support_PowerCheck
#pragma mark - 单例对象
+(instancetype)shareCheckPower{
static Support_PowerCheck *shareGetNowLocation = nil;
static dispatch_once_t once_Token;
dispatch_once(&once_Token, ^{
shareGetNowLocation = [[super allocWithZone:NULL] init];
});
return shareGetNowLocation;
}
+ (instancetype)allocWithZone:(struct _NSZone *)zone{
return [Support_PowerCheck shareCheckPower];
}
//对对象使用copy也是返回唯一实例
-(id)copyWithZone:(NSZone *)zone {
return [Support_PowerCheck shareCheckPower];//return _instance;
}
//对对象使用mutablecopy也是返回唯一实例
-(id)mutableCopyWithZone:(NSZone *)zone {
return [Support_PowerCheck shareCheckPower]; ;
}
-(NSInteger)checkIsHavepower_All{
//推送 定位 相册 相机 通讯录
NSInteger nowPowerStatu = [self checkIsHavePower_PushNotification];
if (nowPowerStatu == PowerStateRefuse) {
return PowerStateRefuse;
}
NSInteger nowPower = 0;
while (nowPower < 5) {
switch (nowPower) {
case 0:
//推送
nowPowerStatu = [self checkIsHavePower_PushNotification];
break;
case 1:
//定位
nowPowerStatu = [self checkIsHavePower_Location];
if (nowPowerStatu == PowerStateUndefined) {
nowPowerStatu = PowerStateAllow;
}
break;
case 2:
//相册
nowPowerStatu = [self checkIsHavePower_Photo:^(bool IsAllow) {
}];
if (nowPowerStatu == PowerStateUndefined) {
[self showSelectPowerAlert:nil whichPower:2];
}
break;
case 3:
//相机
nowPowerStatu = [self checkIsHavePower_Camera:^(bool IsAllow) {
}];
break;
case 4:
//通讯录
nowPowerStatu = [self checkIsHavePower_Contacts:^(bool IsAllow) {
}];
break;
default:
break;
}
if (nowPowerStatu != PowerStateAllow) {
return PowerStateRefuse;
}
nowPower ++;
}
return nowPowerStatu;
}
-(NSInteger)checkIsHavePower_Push_Location{
NSInteger pushInteger = [self checkIsHavePower_PushNotification];
NSInteger locationInteger;
if (pushInteger == PowerStateRefuse) {
return PowerStateRefuse;
}else if(pushInteger == PowerStateAllow){
locationInteger = [self checkIsHavePower_Location];
if (locationInteger == PowerStateRefuse) {
return PowerStateRefuse;
}else if(locationInteger == PowerStateAllow){
return PowerStateAllow;
}
}
return PowerStateAllow;
}
-(NSInteger )checkIsHavePower_Location{
CLAuthorizationStatus authorizationStatu_Location = [CLLocationManager authorizationStatus];
/*
kCLAuthorizationStatusNotDetermined 未选择
kCLAuthorizationStatusRestricted 受限制
kCLAuthorizationStatusDenied 已拒绝
kCLAuthorizationStatusAuthorized 已经明确使用定位功能
kCLAuthorizationStatusAuthorizedWhenInUse 在使用期间允许使用定位功能
kCLAuthorizationStatusAuthorizedAlways App始终允许使用定位功能
*/
if (authorizationStatu_Location == kCLAuthorizationStatusRestricted
|| authorizationStatu_Location == kCLAuthorizationStatusDenied ) {
//拒绝
[self showAlert:@"您尚未授予本应用使用定位功能的权限,将导致您无法正常使用地图功能,请前往设置"];
return PowerStateRefuse;
}else if (authorizationStatu_Location == kCLAuthorizationStatusNotDetermined) {
//未选择
self.locationManager = [[CLLocationManager alloc]init];
[self.locationManager requestAlwaysAuthorization];
[self.locationManager requestWhenInUseAuthorization];
return PowerStateUndefined;
}else {
//允许
return PowerStateAllow;
}
}
-(NSInteger)checkIsHavePower_PushNotification{
if ([[UIApplication sharedApplication] currentUserNotificationSettings].types ==UIUserNotificationTypeNone) {
[self showAlert:@"您尚未授予本应用推送消息的权限,将导致您无法接收到重要消息,请前往设置"];
return PowerStateRefuse;
}
return PowerStateAllow;
//#define IOS8 ([[[UIDevice currentDevice] systemVersion] doubleValue] >=8.0 ? YES : NO)
// if (IOS8) { //iOS8以上包含iOS8
// if ([[UIApplication sharedApplication] currentUserNotificationSettings].types ==UIUserNotificationTypeNone) {
// [self showAlert:@"您尚未授予本应用推送消息的权限,请前往设置"];
// return PowerStateRefuse;
// }
// }else{ // ios7 一下
// if ([[UIApplication sharedApplication] enabledRemoteNotificationTypes] == UIRemoteNotificationTypeNone) {
// [self showAlert:@"您尚未授予本应用推送消息的权限,请前往设置"];
// return PowerStateRefuse;
// }
// }
}
-(NSInteger)checkIsHavePower_Photo:(void (^)(bool IsAllow))AllowBlock{
PHAuthorizationStatus status = [PHPhotoLibrary authorizationStatus];
if (status == PHAuthorizationStatusNotDetermined) {
// [self showSelectPowerAlert:AllowBlock whichPower:2];
return PowerStateUndefined;
}else if (status == PHAuthorizationStatusAuthorized){
//允许
return PowerStateAllow;
}else{
[self showAlert:@"您尚未授予本应用使用相册的权限,请前往设置"];
return PowerStateRefuse;
}
}
-(NSInteger)checkIsHavePower_Camera:(void (^)(bool IsAllow))AllowBlock{
AVAuthorizationStatus authStatus = [AVCaptureDevice authorizationStatusForMediaType:AVMediaTypeVideo]; //读取设备授权状态
if(authStatus == AVAuthorizationStatusNotDetermined) { //第一次请求。
[self showSelectPowerAlert:AllowBlock whichPower:1];
return PowerStateUndefined;
}else if(authStatus == AVAuthorizationStatusAuthorized){
//允许
return PowerStateAllow;
}else{
[self showAlert:@"您尚未授予本应用访问相机权限,请前往设置"];
return PowerStateRefuse;
}
}
-(NSInteger)checkIsHavePower_Contacts:(void (^)(bool IsAllow))AllowBlock{
// 判断是否授权
CNAuthorizationStatus authorizationStatus = [CNContactStore authorizationStatusForEntityType:CNEntityTypeContacts];
if (authorizationStatus == CNAuthorizationStatusNotDetermined) {
[self showSelectPowerAlert:AllowBlock whichPower:0];
return PowerStateUndefined;
}else if (authorizationStatus == CNAuthorizationStatusAuthorized){
return PowerStateAllow;
}else{
[self showAlert:@"您尚未授予本应用访问通讯录的权限,请前往设置"];
return PowerStateRefuse;
}
}
-(void)showAlert:(NSString *)alertStr{
UIAlertController *vc = [UIAlertController alertControllerWithTitle:nil message:alertStr preferredStyle:UIAlertControllerStyleAlert];
UIAlertAction *sureAction = [UIAlertAction actionWithTitle:@"前往设置" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
[vc dismissViewControllerAnimated:YES completion:nil];
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:UIApplicationOpenSettingsURLString]];
}];
// UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) {
//
// }];
[vc addAction:sureAction];
// [vc addAction:cancelAction];
if ([self getCurrentVC]) {
[[self getCurrentVC] presentViewController:vc animated:YES completion:nil];
}
}
-(void)showSelectPowerAlert:(void(^)(bool IsAllow))AllowBlock whichPower:(NSInteger)whichPower{
switch (whichPower) {
case 0:
{
// 联系人
CNContactStore *store = [[CNContactStore alloc]init];
[store requestAccessForEntityType:CNEntityTypeContacts completionHandler:^(BOOL granted, NSError * _Nullable error) {
AllowBlock(granted);
}];
}
break;
case 1://相机
{
[AVCaptureDevice requestAccessForMediaType:AVMediaTypeVideo completionHandler:^(BOOL granted) {
if (AllowBlock) {
AllowBlock(granted);
}
}];
}
break;
case 2://相册
{
[PHPhotoLibrary requestAuthorization:^(PHAuthorizationStatus status) {
if(status == PHAuthorizationStatusAuthorized) {
dispatch_async(dispatch_get_main_queue(), ^{
// 用户点击 "OK"
if (AllowBlock) {
AllowBlock(YES);
}
});
} else {
dispatch_async(dispatch_get_main_queue(), ^{
// 用户点击 不允许访问
if (AllowBlock) {
AllowBlock(NO);
}
});
}
}];
}
break;
default:
break;
}
}
//获取当前屏幕显示的viewcontroller
- (UIViewController *)getCurrentVC
{
UIViewController *result = nil;
UIWindow * window = [[UIApplication sharedApplication] keyWindow];
if (window.windowLevel != UIWindowLevelNormal)
{
NSArray *windows = [[UIApplication sharedApplication] windows];
for(UIWindow * tmpWin in windows)
{
if (tmpWin.windowLevel == UIWindowLevelNormal)
{
window = tmpWin;
break;
}
}
}
UIView *frontView = [[window subviews] objectAtIndex:0];
id nextResponder = [frontView nextResponder];
if ([nextResponder isKindOfClass:[UIViewController class]]){
result = nextResponder;
}else{
result = window.rootViewController;
}
if ([result isKindOfClass:[UITabBarController class]]) {
result = [(UITabBarController *)result selectedViewController];
}
if ([result isKindOfClass:[UINavigationController class]]) {
result = [(UINavigationController *)result topViewController];
}
return result;
}
@end