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

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.

Related

How to use PHAuthorizationStatusLimited in iOS 14

In order to fetch photo's creationDate, so use requestAuthorizationForAccessLevel before show PHPickerViewController.
PHAccessLevel level = PHAccessLevelReadWrite;
[PHPhotoLibrary requestAuthorizationForAccessLevel:level handler:^(PHAuthorizationStatus status) {
if (status == PHAuthorizationStatusLimited || status == PHAuthorizationStatusAuthorized) {
dispatch_async(dispatch_get_main_queue(), ^{
PHPickerConfiguration *configuration = [[PHPickerConfiguration alloc] initWithPhotoLibrary:[PHPhotoLibrary sharedPhotoLibrary]];
configuration.filter = [PHPickerFilter imagesFilter];
configuration.selectionLimit = 1;
PHPickerViewController *picker = [[PHPickerViewController alloc] initWithConfiguration:configuration];
picker.delegate = self;
[self showViewController:picker sender:nil];
});
}
}];
although status is .limited, but iOS 14 still display all images.
How can i get only limited photos with PHPickerViewController?
So a couple of things got changed in iOS 14, let's see step by step
1. How to read PHPhotoLibrary access permission status
Old
let status = PHPhotoLibrary.authorizationStatus()
New
let status = PHPhotoLibrary.authorizationStatus(for: .readWrite)
2. How to request PHPhotoLibrary access permission
Old
PHPhotoLibrary.requestAuthorization { status in
//your code
}
New
PHPhotoLibrary.requestAuthorization(for: .readWrite) { status in
switch status {
case .limited:
print("limited access granted")
default:
print("denied, .restricted ,.authorized")
}
}
It is your responsibility to show gallery like below sample code in case of user granted you limited permission
if status == .limited {
PHPhotoLibrary.shared().presentLimitedLibraryPicker(from: self)
}
When you presentLimitedLibraryPicker the selected images from the previous session would be already marked check, along with a message on top of screen- "Select more photos or deselect to remove access"
In-case the user granted you limited access still you present the normal gallery using UIImagePickerController or a third party library like BSImagePicker, a gallery with all pictures would be shown even you can select and import into your app but in Xcode 12 console it will show warnings as below
Failed to decode image
[ImageManager] Failed to get sandbox extension for url: file///filepath/5003.JPG, error: Error Domain=com.apple.photos.error Code=41008 "Invalid asset uuid for client" UserInfo={NSLocalizedDescription=Invalid asset uuid for client}

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

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 Ask Permission Before Used Page [duplicate]

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
}

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
}

Resources