美文网首页iOSiOS Developer收藏技术点
iOS中各个权限功能提示弹框

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

作者: 程序渣渣猿 | 来源:发表于2019-03-10 00:07 被阅读0次

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

    相关文章

      网友评论

        本文标题:iOS中各个权限功能提示弹框

        本文链接:https://www.haomeiwen.com/subject/qkhypqtx.html