iOS中各个权限功能提醒弹框
1. 麦克风权限
- 单存检测能否有麦克风权限,并不会弹出能否允许弹出权限提醒框
#import <AVFoundation/AVFoundation.h>/** 判断当前是有语音权限,但是不会弹出能否允许弹出权限 (需要在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 <AVFoundation/AVFoundation.h>/** 判断当前是有语音权限,会弹出能否允许弹出权限 (需要在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 <Photos/Photos.h>#import <AssetsLibrary/AssetsLibrary.h>/** 判断相册权限开关,但是不会弹出能否允许弹出权限 (需要在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 <Photos/Photos.h>/** 判断相册权限开关,会弹出能否允许弹出权限 (需要在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 <AVFoundation/AVFoundation.h>/** 判断相机权限开关,但是不会弹出能否允许弹出权限 (需要在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 <AVFoundation/AVFoundation.h>/** 判断相机权限开关,会弹出能否允许弹出权限 (需要在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 <UserNotifications/UserNotifications.h>#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 进行判断,能否允许权限 }}
说明
1. 本站所有资源来源于用户上传和网络,如有侵权请邮件联系站长!
2. 分享目的仅供大家学习和交流,您必须在下载后24小时内删除!
3. 不得使用于非法商业用途,不得违反国家法律。否则后果自负!
4. 本站提供的源码、模板、插件等等其他资源,都不包含技术服务请大家谅解!
5. 如有链接无法下载、失效或广告,请联系管理员处理!
6. 本站资源售价只是摆设,本站源码仅提供给会员学习使用!
7. 如遇到加密压缩包,请使用360解压,如遇到无法解压的请联系管理员
开心源码网 » iOS中各个权限功能提醒弹框
1. 本站所有资源来源于用户上传和网络,如有侵权请邮件联系站长!
2. 分享目的仅供大家学习和交流,您必须在下载后24小时内删除!
3. 不得使用于非法商业用途,不得违反国家法律。否则后果自负!
4. 本站提供的源码、模板、插件等等其他资源,都不包含技术服务请大家谅解!
5. 如有链接无法下载、失效或广告,请联系管理员处理!
6. 本站资源售价只是摆设,本站源码仅提供给会员学习使用!
7. 如遇到加密压缩包,请使用360解压,如遇到无法解压的请联系管理员
开心源码网 » iOS中各个权限功能提醒弹框