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 进行判断,能否允许权限 }}

Logo

为开发者提供学习成长、分享交流、生态实践、资源工具等服务,帮助开发者快速成长。

更多推荐