How to use PHAuthorizationStatusLimited in iOS 14 - ios14

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}

Related

Remove Select Photos... option on PHPhotoLibrary.requestAuthorization

How can I remove the Select Photos... option when requesting for access permission in photo library?
I use PHPhotoLibrary.requestAuthorization to create a access request and it shows me this.
I want to remove the Select Photos... option because the function of this feature is to save a image in a custom photo album.
This is my code for performing a permission request.
if PHPhotoLibrary.authorizationStatus() != PHAuthorizationStatus.authorized {
PHPhotoLibrary.requestAuthorization({ (status: PHAuthorizationStatus) in
// save depending on status
})
} else {
// save image
}

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

Detect permission of media library ios

In my app, I want to detect that if user give the permission to his media library or not. User may denied media library permission when system popup ask or later from setting. Is there any way to detect the status of media library permission?
Here is my code that access list of songs.
MPMediaQuery *everything = [MPMediaQuery songsQuery];
NSArray *songArray = [everything items];
Please see below screenshot where user can change Media Library permissions.
-(void) checkMediaLibraryPermissions {
[MPMediaLibrary requestAuthorization:^(MPMediaLibraryAuthorizationStatus status){
switch (status) {
case MPMediaLibraryAuthorizationStatusNotDetermined: {
// not determined
break;
}
case MPMediaLibraryAuthorizationStatusRestricted: {
// restricted
break;
}
case MPMediaLibraryAuthorizationStatusDenied: {
// denied
break;
}
case MPMediaLibraryAuthorizationStatusAuthorized: {
// authorized
break;
}
default: {
break;
}
}
}];
}
Temporarily, i solved my problem by checking songArray object in below code
MPMediaQuery *everything = [MPMediaQuery songsQuery];
NSArray *songArray = [everything items];
If, user denied permission then songArray object is always nil, but if user allows permission to access to Media Library then songArray object have array of songs. Even if there will be no songs in device but user give permission to access Media Library then there will be array with 0 count.
Swift 4 access check. The simple solution is as follows, and you can alter to include the other alternatives however in my case it was all access or nothing.
private func checkPermissionForMusic() -> Bool {
switch MPMediaLibrary.authorizationStatus() {
case .authorized:
return true
default:
return false
}
}
Caution about using the above solutions - they do perform as a block statement and do not return a value (return true or return "authorised") on the same thread; the result is handled on a background thread. If you decide to use the suggestions above, use a handler (call another function) to handle the result you're expecting. This solution on the other hand tells you immediately if you have access or not. No waiting required.
More info is available in the Apple Docs

App crashes on enabling Camera Access from Settings iOS 9

I've read this almost duplicate question: App crashes on enabling Camera Access from Settings iOS 8 , but my issue isn't solved there.
After initially denying permission for photo library access, I deep link to settings. After enabling access to photos, the app crashes with SIGKILL, which is expected according to Apple specs.
Upon returning to app via back button in status bar one of two things happens depending on whether it's simulator or device:
Simulator: PHAuthorizationStatus does not reflect new status
Device: App is frozen
How can I fix this?
P.S. The code is as follows
if (UIImagePickerController.isSourceTypeAvailable(.PhotoLibrary)) {
if (UIImagePickerController.isSourceTypeAvailable(.PhotoLibrary)) {
let status = PHPhotoLibrary.authorizationStatus()
if (status == .Authorized) {
self.launchGalleryPicker()
} else if (status == .NotDetermined) {
PHPhotoLibrary.requestAuthorization {
(authStatus) in
if (authStatus == .Authorized) {
self.launchGalleryPicker()
}
}
} else {
print("Doesn't work :(")
}
}
}
Where is your code? Put it in viewDidLoad so that when you come back to the app from the back button in the status bar, your view with load and will check the authorization status again.

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