所以我需要在用户给出(或拒绝)使用麦克风的权限后调用某个函数.
我已经看到了这个:
- [[AVAudioSession sharedInstance] requestRecordPermission:^(BOOL granted) {
- if (granted) {
- // Microphone enabled code
- [self someFunction];
- }
- else {
- // Microphone disabled code
- }
- }];
但是,这仅用于检测当前状态.
如果当前状态为“no”并且弹出窗口显示且用户提供权限 – 则不会调用该函数.那是因为在执行此操作的那一刻,权限是“否”,直到我们下次运行代码时才会调用该函数.
谁知道怎么做?
编辑:
忘了提它它必须是iOS 7.0兼容的解决方案.
解决方法
在iOS 8中引入的AVAudioSession方法是recordPermission.这将返回名为AVAudioSessionRecordPermission的枚举.您可以使用开关来确定是否已向用户显示权限警报.这样,只有在没有呈现给用户时才调用requestRecordPermission,因此权限块可以假定在用户第一次允许或不允许权限之后执行它.
一个例子是 – 像 –
- 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;
- }