Nalov
Nalov

Reputation: 638

PhotoLibrary access .notDetermined, when denying then enabling

If I deny at first, then go to settings and allow in settings, in both cases the status is notDetermined, instead of denied then authorized. Why is that happening?

It doesn't save the image when i click "Don't allow", but status becomes .notDetermined not .denied .
It saves, after i go to settings->Photos, uncheck "Never" and check "Add Photos Only". But the status stays .notDetermined, does not become .authorized

func save(){
        guard let image = imageView.image else {return}
        UIImageWriteToSavedPhotosAlbum(image, self, nil, nil)

        let status = PHPhotoLibrary.authorizationStatus()

        switch status {

        case .authorized:
            print("authorized")
            return
        case .notDetermined:  
            print("not determined")
        case .denied, .restricted:
            print("denied or restricted")
            //please go to settings and allow access
            promptToSettings()
        }
    }

I am asking permission to save an image to photo library. When the first time the user tries to save, he gets asked: "App would like to add to Photos" "Don't Allow" "Ok" If the user denied then tried to save again,i want to check and if the status is .denied, prompt the user to go to settings and allow. But the code goes to .notDetermined block when the user does not give access the first time. It stays .notDetermined even after in settings the user allows access.

Upvotes: 0

Views: 1260

Answers (1)

Anand
Anand

Reputation: 1946

I downloaded your code and ran it. I was able to experience whatever you said. It always returned Not Determined status. I did a little bit analysis on your code further. Please find my observation below.

  1. In your current code, "PHPhotoLibrary.requestAuthorization" method is not called before reading the authorization status using "PHPhotoLibrary.authorizationStatus" method.
  2. Though "UIImageWriteToSavedPhotosAlbum" in Save method triggers a Photos app permission pop up, it does not help here completely.
  3. So, I called "askForAccessAgain" method in ViewDidLoad method in order to get the permission using "PHPhotoLibrary.requestAuthorization" method first.
  4. After that, whenever i saved the photos using Save method, it returned the correct status, let it be either "Authorized" or "Denied".
  5. If I choose "Never" and "Allow Photos Only", it returned "Denied or Restricted" status. When "Read and Write" is chosen, "authorized" is returned.

So, it looks like, We need to call "PHPhotoLibrary.requestAuthorization" method to get the permission first instead of relying on other Photo access methods ( like UIImageWriteToSavedPhotosAlbum) for getting the permission. Only then, correct status is returned by "PHPhotoLibrary.authorizationStatus".

One more addition info: App Permissions are retained even after deleting the app and reinstalling it. Just take a look on this as well. It might help you in troubleshooting similar issues in future. iPad remembering camera permissions after delete—how to clear?

Please let me know if this has resolved your issues.

Upvotes: 4

Related Questions