23

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 ?

  • @OneManCrew The question does not appear to show any effort toward solving the problem themselves, and it basically is asking for a tutorial. (Hence "overly broad" in the close reason) – Andrew Barber Jun 19 '13 at 15:16

6 Answers6

27

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

if([[AVAudioSession sharedInstance] respondsToSelector:@selector(requestRecordPermission)])
{
    [[AVAudioSession sharedInstance] requestRecordPermission];
}
One Man Crew
  • 9,420
  • 2
  • 42
  • 51
  • 6
    Actually in iOS7 this function requires usage with blocks which can be called like this: [[AVAudioSession sharedInstance] requestRecordPermission:^(BOOL response){ NSLog(@"Allow microphone use response: %d", response); }]; – C0D3 Aug 08 '13 at 19:50
  • Also you might need a #ifdef __IPHONE_7_0 around the above code #endif to compile it in older versions of xcode. – C0D3 Aug 09 '13 at 19:12
  • 1
    The PrivacyPrompts sample app from Apple utilized the requestRecordPermission is not working. What gives? – user523234 Aug 21 '13 at 19:27
  • can you please check this question? http://stackoverflow.com/questions/18957643/requestrecordpermission-does-nothing and provide needful information, Thanks – PK86 Oct 04 '13 at 07:13
12

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];
    }
Alex Bogomolov
  • 121
  • 1
  • 3
7

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).

Idan
  • 9,880
  • 10
  • 47
  • 76
  • It trivial!by the way if you use this method on earlier iOS versions the method will do nothing,because the method is part of the ios7 sdk. – One Man Crew Jun 14 '13 at 07:35
  • 1
    It's not reasonable to use this method with previous SDKs because you won't compile to iOS 7 and you have nothing to do with it. But if you are using the new SDK and target older devices (iOS 6.x for example), If you won't use this check your app would definitely crash! So, Please pay attention to use this super important check. It happens every time Apple adds a new feature. If it was the other way (Apple remove feature) your app won't crash and you would just get a warning that the function is deprecated (no crash). – Idan Jun 14 '13 at 10:56
  • You are very wrong! There is no direct connection between the SDK package with which was built the application and the operating system installed on the device.You can build complete apps with the SDK of IOS7 and it will run on older OS.!! –  Jun 18 '13 at 07:44
  • 2
    I think you misunderstood me. To be plain simple, If you use: requestRecordPermission on SDK before 7.x your app won't compile (any Xcode before Xcode 5). If you use SDK 7.x (Xcode 5.x) and don't implement the check on the answer your app would crash on any iOS before 7.x (iOS 5.x or iOS 6.x) Hope that is more clear now. – Idan Jun 18 '13 at 12:07
  • 1
    I tried it and i don't implement the check on iPhone 4S iOS 6.1 and the app not crash, i think you wrong. –  Jun 19 '13 at 09:39
  • This approach works great on from iOS 7+ on to iOS 9.x. I'll put together a response based on this. – Alex Zavatone Nov 18 '15 at 19:26
3
    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)
    });
Ilker Baltaci
  • 11,644
  • 6
  • 63
  • 79
2

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;
}
Community
  • 1
  • 1
Alex Zavatone
  • 4,106
  • 36
  • 54
0

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")
        }
    })
}
Amos
  • 2,222
  • 1
  • 26
  • 42