So the thing is that I need to call some function after user gives (or declines) a permission to use the microphone.
I already saw this:
[[AVAudioSes
A method of AVAudioSession introduced in iOS 8 is recordPermission. This returns an enum named AVAudioSessionRecordPermission. You could use a switch to determine whether the permission alert has been presented to the user or not. This way you only call requestRecordPermission when it has not been presented to the user, so the permission block can assume it is being executed after the user allows or disallows permission for the first time.
An example would be something like -
AVAudioSessionRecordPermission permissionStatus = [[AVAudioSession sharedInstance] recordPermission];
switch (permissionStatus) {
case AVAudioSessionRecordPermissionUndetermined:{
[[AVAudioSession sharedInstance] requestRecordPermission:^(BOOL granted) {
// CALL YOUR METHOD HERE - as this assumes being called only once from user interacting with permission alert!
if (granted) {
// Microphone enabled code
}
else {
// Microphone disabled code
}
}];
break;
}
case AVAudioSessionRecordPermissionDenied:
// direct to settings...
break;
case AVAudioSessionRecordPermissionGranted:
// mic access ok...
break;
default:
// this should not happen.. maybe throw an exception.
break;
}
If use has not yet given your permission, do the following:
-
if([[AVAudioSession sharedInstance] respondsToSelector:@selector(requestRecordPermission)]) {
[[AVAudioSession sharedInstance] requestRecordPermission];
// Now run your function
}