iOS权限判断

2022-03-10  本文已影响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];
        });
    }
}
上一篇下一篇

猜你喜欢

热点阅读