如何在发布时检查麦克风访问?

Nit*_*tya 7 microphone objective-c ios7

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

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

这是执行此操作的代码:

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
  }
};
Run Code Online (Sandbox Code Playgroud)

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

Location服务也会发生类似的功能.我怎样才能进行麦克风访问?

Mat*_*att 10

用户拒绝您的应用程序的麦克风访问权限后,您无法再次使用权限对话框显示它们.使用保存的设置.相反,您可以提示用户进入他们的设置并进行更改.

[[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];
    }
}];
Run Code Online (Sandbox Code Playgroud)

  • 执行块在后台线程上执行.因此,不建议调用UI元素.请参阅:http://stackoverflow.com/questions/18625738/how-to-detect-microphone-input-permission-refused-in-ios-7 (2认同)