美文网首页
iOS 关于录音权限的一点整理

iOS 关于录音权限的一点整理

作者: 沐北 | 来源:发表于2019-08-16 09:36 被阅读0次

新的项目中,关于录音权限的代码部分还是很早之前写的,之前苹果调整过一次关于权限的审查,导致另一个项目报审的时候因为权限获取的位置打回过,当时对这块的内容进行了修改

这个方法大致思路是这样的,我们用这个方法去返回我们是否有录音的权限,在触发录音操作之前先做是否有权限的判断,如果没有,就向下进行了如果是第一次获取就提示系统框 如果是没有权限就提示跳转设置去开启

代码如下

//录音权限
- (BOOL)canRecord
{
    __block BOOL bCanRecord = YES;
    if ([[[UIDevice currentDevice]systemVersion]floatValue] >= 8.0)
    {
        AVAudioSession *audioSession = [AVAudioSession sharedInstance];
        if ([audioSession respondsToSelector:@selector(requestRecordPermission:)]) {
            [audioSession performSelector:@selector(requestRecordPermission:)
                               withObject:^(BOOL granted)
             {
                 if (granted) {
                     bCanRecord = YES;
                 }else {
                     bCanRecord = NO;
                     UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) {
                     }];
                     UIAlertAction *doneAction = [UIAlertAction actionWithTitle:@"前往开启" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
                         
#ifdef __IPHONE_8_0
                         //跳入当前App设置界面,
                         NSURL *url = [NSURL URLWithString:UIApplicationOpenSettingsURLString];
                         if( [[UIApplication sharedApplication]canOpenURL:url] ) {
                             [[UIApplication sharedApplication] openURL:url]; // iOS 9 的跳转
                         }
#else
                         //适配iOS7 ,跳入系统设置界面
                         NSURL *url = [NSURL URLWithString:@"prefs:General&path=Reset"];
                         if( [[UIApplicationsharedApplication]canOpenURL:url] ) {
                             [[UIApplicationsharedApplication]openURL:url];
                         }
#endif
                     }];
                     NSLog(@"麦克风权限未开启");
                     [self showMessageWithTitle:@"提示" message:@"麦克风权限未开启,请进入系统【设置】>【隐私】>【麦克风】中打开开关,开启麦克风功能" actionArray:@[cancelAction,doneAction] VC:[self getCurrentVC] preferredStyle:UIAlertControllerStyleAlert];
                 }
             }];
        }
    }
    
    return bCanRecord;
}

// 获取当前显示的 vc 的 名字
- (UIViewController *)getCurrentVC {
    UIViewController *result = nil;
    UIWindow * window = [[UIApplication sharedApplication] keyWindow];
    if (window.windowLevel != UIWindowLevelNormal) {
        NSArray *windows = [[UIApplication sharedApplication] windows];
        for(UIWindow * tmpWin in windows) {
            if (tmpWin.windowLevel == UIWindowLevelNormal) {
                window = tmpWin;
                break;
            }
        }
    }
    
    result = window.rootViewController;
    BOOL isFind= NO;
    while (1) {
        
        if (result.presentedViewController) {
            result = result.presentedViewController;
            isFind = YES;
        }
        
        if ([[result class] isSubclassOfClass:[UITabBarController class]]) {
            result = [(UITabBarController*)result selectedViewController];
            isFind = YES;
        }
        
        if ([[result class] isSubclassOfClass:[UINavigationController class]]) {
            result = [(UINavigationController*)result visibleViewController];
            isFind = YES;
        }
        
        if (isFind == NO) {
            break;
        }else{
            isFind = NO;
        }
    }
    
    if (![result isKindOfClass:[UIViewController class]]) {
        return nil;
    }
    
    return result;
}

相关文章

网友评论

      本文标题:iOS 关于录音权限的一点整理

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