How to check for microphone access at time of launch?

痴心易碎 提交于 2020-01-01 09:20:58

问题


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?


回答1:


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];
    }
}];



回答2:


I had the same question. See this response to a related question including sample code for checking and handling permission status using AVAudioSession which you can customize to provide the user experience you want.



来源:https://stackoverflow.com/questions/21142630/how-to-check-for-microphone-access-at-time-of-launch

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!