Jordan H
Jordan H

Reputation: 55845

Detect when user tapped Don't Allow when making changes to photo library

When you want to make a change to a PHAsset, you wrap it up in a performChanges block. You get a success Bool and an error NSError in the completion block. Now I would like to show an alert to the user in the case the request failed. This does the trick:

PHPhotoLibrary.sharedPhotoLibrary().performChanges({ () -> Void in
    let request = PHAssetChangeRequest(forAsset: asset)
    request.creationDate = date
}, completionHandler: { (success: Bool, error: NSError?) -> Void in
    dispatch_async(dispatch_get_main_queue()) {
        if let error = error {
            //present alert
        }
    }
})

The problem is when the user taps Don't Allow it also presents the alert. I don't want to do that, the user intentionally canceled it so there's no need to inform them it failed. But how can I detect that's what has occurred? The error userInfo is nil, it doesn't seem it provides any useful info to detect that case. Am I missing something?

Upvotes: 2

Views: 358

Answers (2)

Jordan H
Jordan H

Reputation: 55845

This is now possible by checking if the error is a PHPhotosError and if so checking its code to see if it's .userCancelled.

PHPhotoLibrary.shared().performChanges({
    let request = PHAssetChangeRequest(forAsset: asset)
    request.creationDate = date
}) { success, error in
    guard let error = error else { return }
    guard (error as? PHPhotosError)?.code != .userCancelled else { return }
    
    DispatchQueue.main.async {
        //present alert
    }
}

Upvotes: 0

Ishwar Hingu
Ishwar Hingu

Reputation: 558

[PHPhotoLibrary requestAuthorization:^(PHAuthorizationStatus status) {

 NSLog(@"%ld",(long)status);

switch (status) {

    case PHAuthorizationStatusAuthorized:

        // code for display photos


          NSLog(@"ffefwfwfwef");

    case PHAuthorizationStatusRestricted:



        break;
    case PHAuthorizationStatusDenied:

       //code for Dont Allow code

        break;
    default:
        break;
}

}];

Upvotes: 0

Related Questions