如何在发布时检查麦克风访问? [英] How to check for microphone access at time of launch?

查看:163
本文介绍了如何在发布时检查麦克风访问?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的应用中,我将使用麦克风进行录制。从iOS7.0开始,用户被要求在开始音频之前检查访问麦克风的权限。

In my app, I will be using a microphone to do some recording. From iOS7.0 onwards, the user is asked to check the permission to access the microphone before starting the audio.

我的应用程序中有一个开始录制按钮。这里首先检查用户的录制许可。

I have a button 'Start Recording' in my app. Here it first checks the user's permission for recording.

以下是执行此操作的代码:

Here's the code to do this:

if([[AVAudioSession sharedInstance] respondsToSelector:@selector(requestRecordPermission:)]) {
  [[AVAudioSession sharedInstance] performSelector:@selector(requestRecordPermission:)
    withObject:permissionBlock];
}
#ifndef __IPHONE_7_0
  typedef void (^PermissionBlock)(BOOL granted);
#endif

PermissionBlock permissionBlock = ^(BOOL granted) {
  NSLog(@"permissionBlock");
  if (granted) {
    [self doActualRecording];
  } else {
    // Warn no access to microphone
  }
};

现在,我想让用户在用户启动应用程序时授权使用麦克风。然后,当用户选择录制按钮时,它会再次发出弹出消息。

Now, I want to ask the user to authorize microphone use as the user starts the app. Then when the user selects Record button, it gives a popup message again.

位置服务会发生类似的功能。如何才能进行麦克风访问?

A similar functionality happens with Location services. How can I do this for microphone access?

推荐答案

一旦用户拒绝为您的应用设置麦克风访问权限,您就无法使用权限对话框。使用保存的设置。相反,您可以提示用户进入他们的设置并进行更改。

Once a user has denied microphone access for your app, you cannot present them with the permissions dialog again. The saved settings are used. Instead you can prompt the user to go into their settings and make the change.

[[AVAudioSession sharedInstance] requestRecordPermission:^(BOOL granted) {
    if (granted) {
        NSLog(@"granted");
    } else {
        NSLog(@"denied");

        UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Microphone Access Denied"
                                                            message:@"You must allow microphone access in Settings > Privacy > Microphone"
                                                           delegate:nil
                                                  cancelButtonTitle:@"OK"
                                                  otherButtonTitles:nil];
       [alert show];
    }
}];

这篇关于如何在发布时检查麦克风访问?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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