0

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.

Nalov
  • 578
  • 5
  • 9
  • Check here - https://developer.apple.com/documentation/photokit/phauthorizationstatus#declarations – Amir Khan Mar 28 '19 at 12:20
  • @Anna Oriova, Did you quit and launch the app or launch it from background to read the authorizationStatus after changing the permission to allow / denied? – Anand Mar 28 '19 at 16:25
  • @Anand I did both ways. – Nalov Mar 28 '19 at 17:11
  • I tested the above code. whenever the permission is changed, the authorizationStatus gets printed correctly for me. It did not print "Not Determined" always as you mentioned. I can think of only one scenario here. Are you by any chance having two apps with same display name and different bundle id installed in the same device? (Actually this is possible. One will dev signed and another can be inhouse signed.) If that is the case, there is a chance that you change permission to one app and see the status in another app. Just a thought.. – Anand Mar 29 '19 at 05:56

1 Answers1

3

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.

Anand
  • 1,820
  • 2
  • 18
  • 25
  • Thank you. I am marking this as best answer, because even after much searching non of the articles made it clear that UIImageWriteToSavedPhotosAlbum is not reliable on this aspect.I read the link that you provided, thank you. It seems that even if the 'status' after not allowing shows right like this(denied) , and goes to denied block, it relies on app permission in settings and still saves the photo. So no luck relying on this status for checking if the user needs prompt or not. – Nalov Mar 30 '19 at 18:28