How do i request mic record permission from user

2020-02-17 09:22发布

I am using the new iOS7 developer SDK and now the app request from the user his permission to record from mic when the App try to record in the first time. enter image description here

My App will record after a countdown,so the user can't see this request. I use this code to check the requestRecordPermission:

[[AVAudioSession sharedInstance] requestRecordPermission:^(BOOL granted) {
            if (granted) {
                // Microphone enabled code
            }
            else {
                // Microphone disabled code
            }
        }];

But how can i trigger the request by myself before i start to record ?

6条回答
▲ chillily
2楼-- · 2020-02-17 09:49

Swift 4:

let session = AVAudioSession.sharedInstance()
if (session.responds(to: #selector(AVAudioSession.requestRecordPermission(_:)))) {
    AVAudioSession.sharedInstance().requestRecordPermission({(granted: Bool)-> Void in
        if granted {
            print("granted")
        } else {
            print("not granted")
        }
    })
}
查看更多
别忘想泡老子
3楼-- · 2020-02-17 09:52

Here is final code snippet that does work for me. It support both Xcode 4 and 5, and works for iOS5+.

#ifndef __IPHONE_7_0
    typedef void (^PermissionBlock)(BOOL granted);
#endif

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

    // iOS7+
    if([[AVAudioSession sharedInstance] respondsToSelector:@selector(requestRecordPermission:)])
    {
        [[AVAudioSession sharedInstance] performSelector:@selector(requestRecordPermission:)
                                              withObject:permissionBlock];
    }
    else
    {
        [self doActualRecording];
    }
查看更多
小情绪 Triste *
4楼-- · 2020-02-17 09:56

Based on https://stackoverflow.com/users/1071887/idan's response.

AVAudioSession *session = [AVAudioSession sharedInstance];

// AZ DEBUG @@ iOS 7+
AVAudioSessionRecordPermission sessionRecordPermission = [session recordPermission];
switch (sessionRecordPermission) {
    case AVAudioSessionRecordPermissionUndetermined:
        NSLog(@"Mic permission indeterminate. Call method for indeterminate stuff.");
        break;
    case AVAudioSessionRecordPermissionDenied:
        NSLog(@"Mic permission denied. Call method for denied stuff.");
        break;
    case AVAudioSessionRecordPermissionGranted:
        NSLog(@"Mic permission granted.  Call method for granted stuff.");
        break;
    default:
        break;
}
查看更多
我命由我不由天
5楼-- · 2020-02-17 10:05

In the new iOS7 it's very simple try this:

if([[AVAudioSession sharedInstance] respondsToSelector:@selector(requestRecordPermission)])
{
    [[AVAudioSession sharedInstance] requestRecordPermission];
}
查看更多
来,给爷笑一个
6楼-- · 2020-02-17 10:06

As "One Man Crew" claimed you can use requestRecordPermission.

Important thing to be aware of is that you must check that requestRecordPermission is implemented. The reason is that if your app would run on older iOS version (iOS 6.x for example) it would crash after this call. To prevent that you must check that this selector is implemented (this is a good practice anyway).

Code should be something like this:

if([[AVAudioSession sharedInstance] respondsToSelector:@selector(requestRecordPermission:)]){
    [[AVAudioSession sharedInstance] requestRecordPermission];
}

Using this method your app would support the new OS and also previous versions of the OS.

I'm using this method every time Apple add more functionality to new OS (that way I can support older versions pretty easy).

查看更多
成全新的幸福
7楼-- · 2020-02-17 10:13
    AVAudioSession.sharedInstance().requestRecordPermission({ (granted) -> Void in
        if !granted
        {
            let microphoneAccessAlert = UIAlertController(title: NSLocalizedString("recording_mic_access",comment:""), message: NSLocalizedString("recording_mic_access_message",comment:""), preferredStyle: UIAlertControllerStyle.Alert)

            var okAction = UIAlertAction(title: NSLocalizedString("OK",comment:""), style: UIAlertActionStyle.Default, handler: { (alert: UIAlertAction!) -> Void in
                UIApplication.sharedApplication().openURL(NSURL(string: UIApplicationOpenSettingsURLString)!)
            })


            var cancelAction = UIAlertAction(title: NSLocalizedString("Cancel",comment:""), style: UIAlertActionStyle.Cancel, handler: { (alert: UIAlertAction!) -> Void in

            })
            microphoneAccessAlert.addAction(okAction)
            microphoneAccessAlert.addAction(cancelAction)
            self.presentViewController(microphoneAccessAlert, animated: true, completion: nil)
            return
        }
        self.performSegueWithIdentifier("segueNewRecording", sender: nil)
    });
查看更多
登录 后发表回答