2

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?

Jordan H
  • 52,571
  • 37
  • 201
  • 351
  • Try looking at the error's code. There is probably a specific code used to indicate the user denied access. – rmaddy Mar 22 '16 at 23:55
  • @rmaddy Thanks, the `error.code` is `-1`, which appears to be a generic error `The operation couldn’t be completed. (Cocoa error -1.)` – Jordan H Mar 23 '16 at 01:04

2 Answers2

0

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

}];

Ishwar Hingu
  • 562
  • 7
  • 14
  • PHAuthorizationStatus is different. Authorization status is not related to the approval of photo content editing. – Jordan H Apr 19 '16 at 13:21
0

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
    }
}
Jordan H
  • 52,571
  • 37
  • 201
  • 351