iOS Check If Application Has Access to Microphone

iOS check if application has access to microphone

In iOS7 there is no way to get the current status of microphone authorization.They have given the enum in iOS8 as AVAudioSessionRecordPermission

In iOS7 you have to request permission every time with

[[AVAudioSession sharedInstance] requestRecordPermission:^(BOOL granted) {
if (granted) {
NSLog(@"Permission granted");
}
else {
NSLog(@"Permission denied");
}
}];

The same question has been asked before but there is no such api with which you know current status as in iOS8

You can refer Check for mic permission on iOS 7 without showing prompt

Solution:

Another option is you can show the popup or ask for permission first time and save the states of user option selected in NSUserDefaults and than onwards do not ask for permission.
From docs you explicitly do not need to call this if each you do not need to get the permission of user.It will automatically called by AVAudioSession first time when you try to record

Recording audio requires explicit permission from the user. The first
time your app’s audio session attempts to use an audio input route
while using a category that enables recording (see “Audio Session
Categories”), the system automatically prompts the user for
permission; alternatively, you can call requestRecordPermission: to
prompt the user at a time of your choosing

How to detect user giving microphone permission on iOS?

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

Show microphone permission when denied

It's not possible to show the system alert if the user has already denied.
The best you can do is to check the permission and if they're denied show an alert with a button that opens the app settings.

func askPermissionIfNeeded() {
switch AVAudioSession.sharedInstance().recordPermission {
case undetermined:
askMicrophoneAuthorization()
case denied:
let alert = UIAlertController(title: "Error", message: "Please allow microphone usage from settings", preferredStyle: .alert)
alert.addAction(UIAlertAction(title: "Open settings", style: .default, handler: { action in
UIApplication.shared.open(URL(string: UIApplication.openSettingsURLString)!)
}))
alert.addAction(UIAlertAction(title: "Cancel", style: .cancel, handler: nil))
present(alert, animated: true, completion: nil)
case granted:
goToNextStep()
}
}

Permission access to microphone of iOS device ios

enter image description hereFirst of all add "Privacy - Microphone Usage Description" property in your info.plist.
Than add following code ==>

fow swift =>

import AVFoundation

switch AVAudioSession.sharedInstance().recordPermission() {
case AVAudioSessionRecordPermission.granted:
print("Permission granted")
case AVAudioSessionRecordPermission.denied:
print("Pemission denied")
case AVAudioSessionRecordPermission.undetermined:
print("Request permission here")
AVAudioSession.sharedInstance().requestRecordPermission({ (granted) in
// Handle granted
})
}

for Objective-C ==>

#include <AVFoundation/AVFoundation.h>

-(void)askForMicrophonePermission {
switch ([[AVAudioSession sharedInstance] recordPermission]) {
case AVAudioSessionRecordPermissionGranted:
break;
case AVAudioSessionRecordPermissionDenied:
break;
case AVAudioSessionRecordPermissionUndetermined:
// This is the initial state before a user has made any choice
// You can use this spot to request permission here if you want
[[AVAudioSession sharedInstance]requestRecordPermission:^(BOOL granted) {
// Check for granted
}];
break;
default:
break;
}

}

Let me know if you still can't do it.

Permission access to microphone of iOS device ios

enter image description hereFirst of all add "Privacy - Microphone Usage Description" property in your info.plist.
Than add following code ==>

fow swift =>

import AVFoundation

switch AVAudioSession.sharedInstance().recordPermission() {
case AVAudioSessionRecordPermission.granted:
print("Permission granted")
case AVAudioSessionRecordPermission.denied:
print("Pemission denied")
case AVAudioSessionRecordPermission.undetermined:
print("Request permission here")
AVAudioSession.sharedInstance().requestRecordPermission({ (granted) in
// Handle granted
})
}

for Objective-C ==>

#include <AVFoundation/AVFoundation.h>

-(void)askForMicrophonePermission {
switch ([[AVAudioSession sharedInstance] recordPermission]) {
case AVAudioSessionRecordPermissionGranted:
break;
case AVAudioSessionRecordPermissionDenied:
break;
case AVAudioSessionRecordPermissionUndetermined:
// This is the initial state before a user has made any choice
// You can use this spot to request permission here if you want
[[AVAudioSession sharedInstance]requestRecordPermission:^(BOOL granted) {
// Check for granted
}];
break;
default:
break;
}

}

Let me know if you still can't do it.

How to check for microphone access at time of launch?

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

iOS app does not ask camera and microphone permissions on TestFlight?

To make sure your app has permission before capturing media, follow the steps below.

Configure Your App's Info.plist File

iOS requires that your app provide static messages to display to the user when the system asks for camera or microphone permission:

  • If your app uses device cameras, include the Privacy - Camera Usage
    Description key in your app’s Info.plist file.
  • If your app uses device photo library, include the Privacy - Photo
    Library Usage Description key in your app’s Info.plist file

Verify and Request Authorization for Capture

You can add below code in AppDelegate main file OR while click on capture media to request access authorization.

import AVFoundation

AVCaptureDevice.requestAccess(for: .video) { granted in

if granted {
// Setup your code.
}
}

Apple Ref Link https://developer.apple.com/documentation/avfoundation/cameras_and_media_capture/requesting_authorization_for_media_capture_on_ios

How to check if the user gave permission to use the camera?

You can use the following code for doing the same:

if AVCaptureDevice.authorizationStatusForMediaType(AVMediaTypeVideo) ==  AVAuthorizationStatus.Authorized {
// Already Authorized
} else {
AVCaptureDevice.requestAccessForMediaType(AVMediaTypeVideo, completionHandler: { (granted: Bool) -> Void in
if granted == true {
// User granted
} else {
// User rejected
}
})
}

NOTE:

  1. Make sure that you add the AVFoundation Framework in the Link Binary section of build phases
  2. You should write import AVFoundation on your class for importing AVFoundation

SWIFT 3

if AVCaptureDevice.authorizationStatus(forMediaType: AVMediaTypeVideo) ==  AVAuthorizationStatus.authorized {
// Already Authorized
} else {
AVCaptureDevice.requestAccess(forMediaType: AVMediaTypeVideo, completionHandler: { (granted: Bool) -> Void in
if granted == true {
// User granted
} else {
// User Rejected
}
})
}

Swift 4

if AVCaptureDevice.authorizationStatus(for: .video) ==  .authorized {
//already authorized
} else {
AVCaptureDevice.requestAccess(for: .video, completionHandler: { (granted: Bool) in
if granted {
//access allowed
} else {
//access denied
}
})
}


Related Topics



Leave a reply



Submit