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

查看:293
本文介绍了如何检测麦克风输入权限在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?

我在输出中得到了这个消息:
麦克风输入权限被拒绝 - code>

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.\n\nPlease enable Microphone access for this app in Settings / Privacy / Microphone"
                                       delegate:nil
                              cancelButtonTitle:@"Dismiss"
                              otherButtonTitles:nil] autorelease] show];
            });
        }
    }];
}

EDIT :原来,是在后台线程中执行的,所以不要在那里做任何UI工作,否则你的应用程序可能会挂起。我已经调整了上面的代码。一个客户指出这是值得庆幸的是beta版本。抱歉,错误。

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天全站免登陆