iOS Ask Permission Before Used Page [duplicate] - ios

With the introduction of iOS 7, applications have to request microphone access when they want to record audio.
How do I check if the application has access to the microphone?
In the iOS 8 SDK I can use the AVAudioSessionRecordPermission enum, but how do I check this in iOS 7?
Info:
I don't want to request permission, I just want to check if the app has access to the microphone. (Like Location access):
if ([CLLocationManager authorizationStatus] == kCLAuthorizationStatusDenied) {
// Do something
}

You can check the with recordPermission(), which has been available since iOS 8.
Keep in mind that starting with iOS 10, you must set the NSMicrophoneUsageDescription property in your info.plist for microphone permissions and include a message for the user. This message is shown to the user at time of the request. Finally, if localizing your app, be sure to include your plist strings for translation.
Failure to do so will result in a crash when attempting to access the microphone.
This answer has been cleaned up again for Swift 5.x
import AVFoundation
switch AVAudioSession.sharedInstance().recordPermission {
case .granted:
print("Permission granted")
case .denied:
print("Permission denied")
case .undetermined:
print("Request permission here")
AVAudioSession.sharedInstance().requestRecordPermission({ granted in
// Handle granted
})
#unknown default:
print("Unknown case")
}
Objective-C
I have tested this code with iOS 8 for the purpose of checking for microphone permission and obtaining the current state.
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
break;
default:
break;
}
As always, make sure to import AVFoundation.

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

Swift 3 Complete Solution Code
func checkMicPermission() -> Bool {
var permissionCheck: Bool = false
switch AVAudioSession.sharedInstance().recordPermission() {
case AVAudioSessionRecordPermission.granted:
permissionCheck = true
case AVAudioSessionRecordPermission.denied:
permissionCheck = false
case AVAudioSessionRecordPermission.undetermined:
AVAudioSession.sharedInstance().requestRecordPermission({ (granted) in
if granted {
permissionCheck = true
} else {
permissionCheck = false
}
})
default:
break
}
return permissionCheck
}

There is another way you can try following code for ios 7 and 8 :
let microPhoneStatus = AVCaptureDevice.authorizationStatusForMediaType(AVMediaTypeAudio)
switch microPhoneStatus {
case .Authorized:
// Has access
case .Denied:
// No access granted
case .Restricted:
// Microphone disabled in settings
case .NotDetermined:
// Didn't request access yet
}

[[AVAudioSession sharedInstance] requestRecordPermission:^(BOOL granted) {
if (granted) {
// Microphone enabled code
}
else {
// Microphone disabled code
}
}];
And include <AVFoundation/AVAudioSession.h>

Since none of the other answers here mentioned this, you need to add the permissions to your info.plist. Specifically, add an entry for:
Privacy - Microphone Usage Description
For the String value, enter something like:
(App name) needs access to your microphone.
Otherwise, you get a mysterious crash

What I often end up doing for a quick check on objects working with audio record:
// swift 5
static public func isAuthorized() -> Bool {
return AVCaptureDevice.authorizationStatus(for: .audio) == .authorized
}

import AVFoundation and use the following function
var permissionCheck:Bool = false
switch AVAudioSession.sharedInstance().recordPermission {
case AVAudioSession.RecordPermission.granted:
permissionCheck = true
case AVAudioSession.RecordPermission.denied:
permissionCheck = false
case AVAudioSession.RecordPermission.undetermined:
AVAudioSession.sharedInstance().requestRecordPermission({ (granted) in
if granted {
permissionCheck = true
} else {
permissionCheck = false
}
})
default:
break
}

Related

Photo Library and Camera are accessible after permission got denied

I'm currently working on an app where I need to access either the photo library or the camera (or even both, it's up to the user).
If I close the app and deny the access to both camera and library for the app.
After restarting my App, it's still possible to access both, I can open and use the camera or even pick a photo from the library.
How is this possible, I thought Apple would restrict the access.
I dont think so. Apple always restrict use of camera or photo library if access is denied. Please use following code to check current status and verify it.
import Photos
if type == .CAMERA{
if AVCaptureDevice.authorizationStatus(forMediaType: AVMediaTypeVideo) == AVAuthorizationStatus.authorized {
completionHander(true) //Allowed
} else {
//Dont Know
AVCaptureDevice.requestAccess(forMediaType: AVMediaTypeVideo, completionHandler: { (granted: Bool) -> Void in
completionHander(granted)
})
}
}else{
let status = PHPhotoLibrary.authorizationStatus()
switch status {
case .authorized:
completionHander(true)//Allowed
break
case .denied, .restricted :
completionHander(false)//Not Allowed
break
case .notDetermined:
//Dont Know
PHPhotoLibrary.requestAuthorization { status in
switch status {
case .authorized:
completionHander(true)
break
case .denied, .restricted:
completionHander(false)
break
case .notDetermined:
completionHander(false)
break
}
}
}
}
Okay I know the answer...
I just didn't get the concept.
If I deny the access to the photoLibrary it is not possible to save images but, if Camera access is granted I can still use the photoLibrary.
If camera access is denied, I still get the alert with the context from the info.plist

Ask Permissions before use in-built features in iOS

I am using camera, microPhone and current location in my iOS App like SOS Button functionality. Its working fine but when I press this button first time its asking permissions for using these in-built features. How to ask permissions before using this SOS Button first time.
you can check in app delegate when app is launch so your permission is granted.
let mediatype = AVMediaType.video
let AuthoriseStatus = AVCaptureDevice.authorizationStatus(for: cameraMediaType)
switch AuthoriseStatus {
case .denied: break
case .authorized: break
case .restricted: break
case .notDetermined:
// permission prompt
AVCaptureDevice.requestAccess(for: mediatype) { granted in
if granted {
print("Granted access to \(mediatype)")
} else {
print("Denied access to \(mediatype)")
}
}
}

Requesting music library permissions swift or obj-c

I want to request music library permissions right when my application opens the first time.
The privacy in my info.plist are:
Privacy - Media library usage description
and
Privacy - music usage description
I would prefer this to be in my app delegate did finish launching with options but it is okay if it is in my viewDidLoad of the first viewController of my app.
You can request for permission like
let status = MPMediaLibrary.authorizationStatus()
switch status {
case .authorized:
// Get Media
case .notDetermined:
MPMediaLibrary.requestAuthorization() { status in
if status == .authorized {
DispatchQueue.main.async {
// // Get Media
}
}
}
}

iOS check if application has access to microphone

With the introduction of iOS 7, applications have to request microphone access when they want to record audio.
How do I check if the application has access to the microphone?
In the iOS 8 SDK I can use the AVAudioSessionRecordPermission enum, but how do I check this in iOS 7?
Info:
I don't want to request permission, I just want to check if the app has access to the microphone. (Like Location access):
if ([CLLocationManager authorizationStatus] == kCLAuthorizationStatusDenied) {
// Do something
}
You can check the with recordPermission(), which has been available since iOS 8.
Keep in mind that starting with iOS 10, you must set the NSMicrophoneUsageDescription property in your info.plist for microphone permissions and include a message for the user. This message is shown to the user at time of the request. Finally, if localizing your app, be sure to include your plist strings for translation.
Failure to do so will result in a crash when attempting to access the microphone.
This answer has been cleaned up again for Swift 5.x
import AVFoundation
switch AVAudioSession.sharedInstance().recordPermission {
case .granted:
print("Permission granted")
case .denied:
print("Permission denied")
case .undetermined:
print("Request permission here")
AVAudioSession.sharedInstance().requestRecordPermission({ granted in
// Handle granted
})
#unknown default:
print("Unknown case")
}
Objective-C
I have tested this code with iOS 8 for the purpose of checking for microphone permission and obtaining the current state.
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
break;
default:
break;
}
As always, make sure to import AVFoundation.
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
Swift 3 Complete Solution Code
func checkMicPermission() -> Bool {
var permissionCheck: Bool = false
switch AVAudioSession.sharedInstance().recordPermission() {
case AVAudioSessionRecordPermission.granted:
permissionCheck = true
case AVAudioSessionRecordPermission.denied:
permissionCheck = false
case AVAudioSessionRecordPermission.undetermined:
AVAudioSession.sharedInstance().requestRecordPermission({ (granted) in
if granted {
permissionCheck = true
} else {
permissionCheck = false
}
})
default:
break
}
return permissionCheck
}
There is another way you can try following code for ios 7 and 8 :
let microPhoneStatus = AVCaptureDevice.authorizationStatusForMediaType(AVMediaTypeAudio)
switch microPhoneStatus {
case .Authorized:
// Has access
case .Denied:
// No access granted
case .Restricted:
// Microphone disabled in settings
case .NotDetermined:
// Didn't request access yet
}
[[AVAudioSession sharedInstance] requestRecordPermission:^(BOOL granted) {
if (granted) {
// Microphone enabled code
}
else {
// Microphone disabled code
}
}];
And include <AVFoundation/AVAudioSession.h>
Since none of the other answers here mentioned this, you need to add the permissions to your info.plist. Specifically, add an entry for:
Privacy - Microphone Usage Description
For the String value, enter something like:
(App name) needs access to your microphone.
Otherwise, you get a mysterious crash
What I often end up doing for a quick check on objects working with audio record:
// swift 5
static public func isAuthorized() -> Bool {
return AVCaptureDevice.authorizationStatus(for: .audio) == .authorized
}
import AVFoundation and use the following function
var permissionCheck:Bool = false
switch AVAudioSession.sharedInstance().recordPermission {
case AVAudioSession.RecordPermission.granted:
permissionCheck = true
case AVAudioSession.RecordPermission.denied:
permissionCheck = false
case AVAudioSession.RecordPermission.undetermined:
AVAudioSession.sharedInstance().requestRecordPermission({ (granted) in
if granted {
permissionCheck = true
} else {
permissionCheck = false
}
})
default:
break
}

How may I check if my app has access to phone gallery

I have an app in which I take a picture with the camera and store that image into the native gallery. But if the app doesn't have permission for that, I want the user to know that. So how do I check it?
By the way: I store the image into the gallery with:
UIImageWriteToSavedPhotosAlbum(image, nil, nil, nil);
You need to check the status of ALAssetLibrary
make sure you have AssetsLibrary/AssetsLibrary.h included in your file
ALAuthorizationStatus status = [ALAssetsLibrary authorizationStatus];
// check the status for ALAuthorizationStatusAuthorized or ALAuthorizationStatusDenied e.g
if (status != ALAuthorizationStatusAuthorized) {
//show alert for asking the user to give permission
}
Swift 3
import photos
PHPhotoLibrary.requestAuthorization { status in
switch status {
case .authorized:
self.processSnapShotPhotos()
case .restricted:
print("handle restricted")
case .denied:
print("handle denied")
default:
// place for .notDetermined - in this callback status is already determined so should never get here
break
}
}
If you are using photos framework since ALAsset libraries are deprecated from ios 9 you can use PHAuthorizationStatus to check gallery access. You need to import photos framework as well.
#import <Photos/Photos.h>
- (BOOL)hasGalleryPermission
{
BOOL hasGalleryPermission = NO;
PHAuthorizationStatus authorizationStatus = [PHPhotoLibrary authorizationStatus];
if (authorizationStatus == PHAuthorizationStatusAuthorized) {
hasGalleryPermission = YES;
}
return hasGalleryPermission;
}
Note: iOS 6 Only
Is this what you are looking for
[ALAssetsLibrary authorizationStatus] == ALAuthorizationStatusAuthorized;
Other values of authorizationStatus are
ALAuthorizationStatusRestricted, // This application is not authorized to access photo data.
// The user cannot change this application’s status, possibly due to active restrictions
// such as parental controls being in place.
ALAuthorizationStatusDenied, // User has explicitly denied this application access to photos data.
ALAuthorizationStatusAuthorized // User has authorized this application to access photos data.

Resources