美文网首页
iOS权限判断

iOS权限判断

作者: 夜雨聲煩_ | 来源:发表于2022-03-10 16:31 被阅读0次

    麦克风权限

    - (void)handupActionWith:(BOOL)isHandup {
        if (isHandup) {
            [self checkMicrophonePermissionAndRunAction:^(BOOL granted) {
                if (granted) {
                    //连麦事件
                    [QSCommonUtil showMessage:@"已举手,等待主持人接通"];
                }else {
                    [self toastWithStr:@"请在设置中开启麦克风,否则无法举手" completion:^{
                        [self openSetting];
                    }];
                }
            }];
           
        }else {
            //取消连麦事件
            [QSCommonUtil showMessage:@"举手已取消"];
        }
    }
    
    - (void)checkMicrophonePermissionAndRunAction:(void (^)(BOOL granted))action {
        AVAuthorizationStatus status = [AVCaptureDevice authorizationStatusForMediaType:mediaType];
        
        if (status == AVAuthorizationStatusAuthorized) {
            return YES;
        }
        
        if (status == AVAuthorizationStatusRestricted || status == AVAuthorizationStatusDenied) {
            return NO;
        }
        
        //AVAuthorizationStatusNotDetermined 才走下面的逻辑
    
    
        AVAudioSession *audioSession = [AVAudioSession sharedInstance];
        if ([audioSession respondsToSelector:@selector(requestRecordPermission:)]) {
            [audioSession requestRecordPermission:^(BOOL granted) {
                dispatch_async(dispatch_get_main_queue(), ^{
                    if (action) {
                        action(granted);
                    }
                });
            }];
        }
        else {
            if (action) {
                action(YES);
            }
        }
    }
    
    - (void)openSetting {
        NSURL *url = [NSURL URLWithString:UIApplicationOpenSettingsURLString];
        if (@available(iOS 10.0, *)) {
            if ([[UIApplication sharedApplication] respondsToSelector:@selector(openURL:options:completionHandler:)]) {
                [[UIApplication sharedApplication] openURL:url options:@{} completionHandler:NULL];
            } else {
                dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.001 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
                    [[UIApplication sharedApplication] openURL:url];
                });
            }
        }
        else {
            dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.001 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
                [[UIApplication sharedApplication] openURL:url];
            });
        }
    }
    

    相关文章

      网友评论

          本文标题:iOS权限判断

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