iOS中各个权限功能提示弹框

说明

iOS 8之后许多权限都需要我们在info.plist中添加描述说明。随着苹果审核越来越严格,我们的描述也需要更精细化,具体来说就是尽量在描述中说明这个权限是用来做什么的,而不是之前笼统的说我们要使用**功能,被拒的可能性很大。

需要我们描述的权限,我们都可以在【设置】-【隐私】中看到。

  • 定位服务
  • 通讯录
  • 日历
  • 提醒事项
  • 照片
  • 蓝牙共享
  • 麦克风
  • 语音识别
  • 相机
  • 健康
  • HomeKit
  • 媒体与Apple Music
  • 运动与健身
权限名称 权限名称 权限描述 补充说明
Privacy - Microphone Usage Description 麦克风权限 允许应用使用麦克风,用来**功能 --
Privacy - Camera Usage Description 相机权限 允许应用使用相机,用来**功能 --
Privacy - Photo Library Additions Usage Description 相册权限 允许应用访问相册,用来**功能 --
Privacy - Location When In Use Usage Description 定位权限 允许应用在使用中访问您的位置,用来**功能 使用中定位
Privacy - Location Always and When In Use Usage Description 定位权限 允许应用访问您的位置,用来**功能 使用中,后台一直定位

1. 麦克风权限

  • 单存检测是否有麦克风权限,并不会弹出是否允许弹出权限提示框
#import 

/**
 判断当前是有语音权限,但是不会弹出是否允许弹出权限
 (需要在info中配置)Privacy - Microphone Usage Description 允许**访问您的语音,来用于**功能?
 
 @return YES:有权限,NO:没权限
 */
- (BOOL)JX_Device_Permission_AudioAuth {
    AVAuthorizationStatus authStatus = [AVCaptureDevice authorizationStatusForMediaType:AVMediaTypeAudio];
    if (authStatus == AVAuthorizationStatusDenied || authStatus == AVAuthorizationStatusRestricted) {
        return NO;
    }
    return YES;
}
  • 检测是否有权限,如果没有授权过,会弹出是否允许提示框
#import 

/**
 判断当前是有语音权限,会弹出是否允许弹出权限
 (需要在info中配置)Privacy - Microphone Usage Description 允许**访问您的语音,来用于**功能?
 */
- (void)JX_Device_Permission_Check_AudioAuth {
    AVAudioSession *session = [AVAudioSession sharedInstance];
    if ([session respondsToSelector:@selector(requestRecordPermission:)]){
        [session performSelector:@selector(requestRecordPermission:) withObject:^(BOOL granted) {
           // do something
        }];
    }

}

2. 访问相册权限

  • 检测是否有访问相册权限,并不会弹出是否允许访问相册权限提示框
#import 
#import 

/**
 判断相册权限开关,但是不会弹出是否允许弹出权限
 (需要在info中配置)Privacy - Photo Library Additions Usage Description 允许**访问您的相册,来用于**功能
 
 @return YES:有权限,NO:没权限
 */
- (BOOL)JX_Device_Permission_PhotoLibraryAuth {
    
    if ([[UIDevice currentDevice].systemVersion floatValue]  >= 8.0) {
        PHAuthorizationStatus authStatus = [PHPhotoLibrary authorizationStatus];
        if(authStatus == PHAuthorizationStatusDenied || authStatus == PHAuthorizationStatusRestricted) {
            return NO;
        }
    } else if ([[UIDevice currentDevice].systemVersion floatValue] >= 6.0 && [[UIDevice currentDevice].systemVersion floatValue] < 8.0) {
        ALAuthorizationStatus authStatus = [ALAssetsLibrary authorizationStatus];
        if(authStatus == ALAuthorizationStatusDenied || authStatus == ALAuthorizationStatusRestricted) {
            return NO;
        }
    }
    return YES;
}
  • 检测是否有权限,如果没有授权过,会弹出是否允许提示框
#import 

/**
 判断相册权限开关,会弹出是否允许弹出权限
 (需要在info中配置)Privacy - Photo Library Additions Usage Description 允许**访问您的相册,来用于**功能
 */
- (void)JX_Device_Permission_Check_PhotoLibraryAuth{
    
    BOOL auth = [UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypePhotoLibrary];
    if (!auth) return;
    [PHPhotoLibrary requestAuthorization:^(PHAuthorizationStatus status) { //弹出访问权限提示框
        if (status == PHAuthorizationStatusAuthorized) { // 有权限
            dispatch_async(dispatch_get_main_queue(),^{
               // do something
               //  一般操作
              self.imagePickerController.sourceType   = UIImagePickerControllerSourceTypePhotoLibrary;
              [self presentViewController:self.imagePickerController animated:YES completion:nil];
            });
        } else {
            dispatch_async(dispatch_get_main_queue(),^{ // 无权限
               // do something
            });
            
        }
    }];
}

3. 访问相机权限

  • 检测是否有相机拍照权限,并不会弹出是否允许提示框
#import 

/**
 判断相机权限开关,但是不会弹出是否允许弹出权限
 (需要在info中配置)Privacy - Camera Usage Description 允许**访问您的相机,来用于**功能
 
 @return YES:有权限,NO:没权限
 */
- (BOOL)JX_Device_Permission_CameraAuth {
    AVAuthorizationStatus authStatus = [AVCaptureDevice authorizationStatusForMediaType:AVMediaTypeVideo];
    if (authStatus == AVAuthorizationStatusDenied || authStatus == AVAuthorizationStatusRestricted) {
        return NO;
    }
    return YES;
}
  • 检测是否有过授权,如果没有授权过,会弹出是否允许提示框
#import 

/**
 判断相机权限开关,会弹出是否允许弹出权限
 (需要在info中配置)Privacy - Camera Usage Description 允许**访问您的相机,来用于**功能
 */
- (void)JX_Device_Permission_Check_CameraAuth {
    BOOL auth = [UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera];
    if (!auth) permission(NO);
    NSString *mediaType = AVMediaTypeVideo;//读取媒体类型
    [AVCaptureDevice requestAccessForMediaType:mediaType completionHandler:^(BOOL granted) {
        dispatch_async(dispatch_get_main_queue(),^{
            if (granted) { // 授权成功
                // do something
                // 一般会做的操作,跳转到系统的相机
                self.imagePickerController.sourceType   = UIImagePickerControllerSourceTypeCamera;
                [self presentViewController:self.imagePickerController animated:YES completion:nil];
            } else { // 拒绝授权
                // do something
            }
        });
    }];
}

4. 推送权限(远程、本地)

  • 检测是否有推送权限,并不会弹出是否允许提示框

/**
 推送权限开关
 
 @return YES:有权限,NO:没权限
 */
- (BOOL)JX_Device_Permission_NotificationAuth {
    
    if ([[UIDevice currentDevice].systemVersion floatValue] >= 8.0f) {
        UIUserNotificationSettings *setting = [[UIApplication sharedApplication] currentUserNotificationSettings];
        if (UIUserNotificationTypeNone == setting.types) {
            return  NO;
        }
    } else {
        UIRemoteNotificationType type = [[UIApplication sharedApplication] enabledRemoteNotificationTypes];
        if(UIRemoteNotificationTypeNone == type){
            return  NO;
        }
    }
    return YES;
}
  • 检测是否有过授权,如果没有授权过,会弹出是否允许提示框

说明:这里仅仅是检测是否授权过,弹出提示框操作。至于远程注册一些流程请参见另一篇博客。iOS 通知权限(远程通知、本地通知)

#ifdef NSFoundationVersionNumber_iOS_9_x_Max
#import 
#endif

/**
 判断通知权限开关,会弹出是否允许弹出权限(远程、本地)
 */
- (void)JX_Device_Permission_Check_NotificationAuth {
    if ([[UIDevice currentDevice].systemVersion floatValue]  >= 10.0) {
        [[UNUserNotificationCenter   currentNotificationCenter] requestAuthorizationWithOptions:(UNAuthorizationOptionBadge | UNAuthorizationOptionSound | UNAuthorizationOptionAlert) completionHandler:^(BOOL granted, NSError * _Nullable error) {
            dispatch_async(dispatch_get_main_queue(), ^{
                // do something
                // 对granted 进行判断,是否允许权限
            });
        }];
    } else if ([[UIDevice currentDevice].systemVersion floatValue]  >= 8.0) {
        UIUserNotificationSettings *setting = [UIUserNotificationSettings settingsForTypes:UIUserNotificationTypeSound | UIUserNotificationTypeAlert | UIUserNotificationTypeBadge categories:nil];
        [[UIApplication sharedApplication] registerUserNotificationSettings:setting];
        BOOL auth = [self JX_Device_Permission_NotificationAuth];
        // 对auth 进行判断,是否允许权限
    }
}

5. 定位权限

  • 检测定位权限,不会弹出是否允许请求定位框
#import  

/**
 定位权限开关
 (需要在info中配置)Privacy - Location When In Use Usage Description 允许**在应用使用期间访问您的位置,来用于**功能
 (需要在info中配置)Privacy - Location Always and When In Use Usage Description 允许**访问您的位置,来用于**功能
 
 @return YES:有权限,NO:没权限
 */
- (BOOL)JX_Device_Permission_LocationAuth {
    if (![CLLocationManager locationServicesEnabled]) {
        return NO;
    }
    CLAuthorizationStatus CLstatus = [CLLocationManager authorizationStatus];
    if (CLstatus == kCLAuthorizationStatusDenied || CLstatus == kCLAuthorizationStatusRestricted) {
        return NO;
    }
    return YES;
}
  • 检测使用中定位权限,如果没有授权过,会弹出是否授权提示框

说明:这里仅仅是检测是否售全国,弹出提示框操作。详细的描述情况参见另一篇iOS 定位权限(应用使用中定位、一直定位)

#import  

@interface JXDeviceHelper ()

/**
 定位回调
 */
@property (nonatomic,copy) void (^JX_Device_Location)(CLAuthorizationStatus state);
@property (nonatomic, strong) CLLocationManager         *locationManager;       // 定位

@end

/**
 定位权限开关
 (需要在info中配置)Privacy - Location When In Use Usage Description 允许**访问您的位置,来用于**功能
 */
- (void)JX_Device_Permission_Check_LocationAuth_InUse:(CheckPermissionInUseLocationAuth)permission {
    self.locationManager = [[CLLocationManager alloc] init];
    [self.locationManager requestWhenInUseAuthorization];
    __weak typeof(self) weakSelf = self;
    self.locationManager.delegate    = self;
    [self setJX_Device_Location:^(CLAuthorizationStatus state) {
        dispatch_async(dispatch_get_main_queue(), ^{
            BOOL auth = [weakSelf JX_Device_Permission_LocationAuth];
            permission(auth);
        });
    }];
}


#pragma mark - 定位回调(CLLocationManagerDelegate)
- (void)locationManager:(CLLocationManager *)manager didChangeAuthorizationStatus:(CLAuthorizationStatus)status{
    if (status == kCLAuthorizationStatusNotDetermined) return;
    if (self.JX_Device_Location) {
        self.JX_Device_Location(status);
    }
}

  • 检测后台定位权限,如果没有售全国,会弹出是否授权提示框

说明:这里仅仅是检测是否售全国,弹出提示框操作。详细的描述情况参见另一篇iOS 定位权限(应用使用中定位、一直定位)

#import  

@interface JXDeviceHelper ()

/**
 定位回调
 */
@property (nonatomic,copy) void (^JX_Device_Location)(CLAuthorizationStatus state);
@property (nonatomic, strong) CLLocationManager         *locationManager;       // 定位

@end

/**
 定位权限开关
 (需要在info中配置)Privacy - Location Always and When In Use Usage Description 允许**访问您的位置,来用于**功能
 */
- (void)JX_Device_Permission_Check_LocationAuth_Always:(CheckPermissionAlwaysLocationAuth)permission {
    self.locationManager = [[CLLocationManager alloc] init];
    [self.locationManager requestAlwaysAuthorization];
    __weak typeof(self) weakSelf = self;
    self.locationManager.delegate    = self;
    [self setJX_Device_Location:^(CLAuthorizationStatus state) {
        dispatch_async(dispatch_get_main_queue(), ^{
            BOOL auth = [weakSelf JX_Device_Permission_LocationAuth];
            permission(auth);
        });
    }];
}


#pragma mark - 定位回调(CLLocationManagerDelegate)
- (void)locationManager:(CLLocationManager *)manager didChangeAuthorizationStatus:(CLAuthorizationStatus)status{
    if (status == kCLAuthorizationStatusNotDetermined) return;
    if (self.JX_Device_Location) {
        self.JX_Device_Location(status);
    }
}

你可能感兴趣的:(iOS中各个权限功能提示弹框)