如何检测 iOS 7 中拒绝的麦克风输入权限 [英] How to detect microphone input permission refused in iOS 7

查看:15
本文介绍了如何检测 iOS 7 中拒绝的麦克风输入权限的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想检测用户何时拒绝了我的 iOS 应用程序的麦克风权限.当我尝试录制麦克风时,我只得到这个值:-120.000000 db

I would like to detect when a user refused the microphone permission on my iOS application. I only get this value when I try to record the microphone: -120.000000 db

但在此之前,我必须设置一个 AVAudioSession.还有其他功能吗?

But before to get this I have to set up an AVAudioSession. Is there another function?

我在输出中收到了这条消息:麦克风输入权限被拒绝 - 只会录制静音

And I got this message in the output: Microphone input permission refused - will record only silence

谢谢.

推荐答案

如果您仍在使用 iOS SDK 6.0 进行编译(就像我一样),那么作为 requestRecordPermission 方法,您必须比 @Luis E. Prado 更间接一些不存在.

If you are still compiling with iOS SDK 6.0 (as I am) you have to be a bit more indirect than @Luis E. Prado, as the requestRecordPermission method doesn't exist.

这是我如何做到的.如果您使用的是 ARC,请移除自动释放位.在 iOS6 上什么也没有发生,在 iOS7 上要么记录麦克风已启用"消息,要么弹出警报.

Here's how I did it. Remove the autorelease bit if you're using ARC. On iOS6 nothing happens, and on iOS7 either the 'microphone is enabled' message is logged or the alert is popped up.

AVAudioSession *session = [AVAudioSession sharedInstance];
if ([session respondsToSelector:@selector(requestRecordPermission:)]) {
    [session performSelector:@selector(requestRecordPermission:) withObject:^(BOOL granted) {
        if (granted) {
            // Microphone enabled code
            NSLog(@"Microphone is enabled..");
        }
        else {
            // Microphone disabled code
            NSLog(@"Microphone is disabled..");

            // We're in a background thread here, so jump to main thread to do UI work.
            dispatch_async(dispatch_get_main_queue(), ^{
                [[[[UIAlertView alloc] initWithTitle:@"Microphone Access Denied"
                                        message:@"This app requires access to your device's Microphone.

Please enable Microphone access for this app in Settings / Privacy / Microphone"
                                       delegate:nil
                              cancelButtonTitle:@"Dismiss"
                              otherButtonTitles:nil] autorelease] show];
            });
        }
    }];
}

编辑:原来 withObject 块是在后台线程中执行的,所以不要在那里做任何 UI 工作,否则你的应用程序可能会挂起.我已经调整了上面的代码.一位客户在测试版发布时指出了这一点.为错误道歉.

EDIT: It turns out that the withObject block is executed in a background thread, so DO NOT do any UI work in there, or your app may hang. I've adjusted the code above. A client pointed this out on what was thankfully a beta release. Apologies for the mistake.

这篇关于如何检测 iOS 7 中拒绝的麦克风输入权限的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆