1

i am trying to determine if user gives access or not add photos only :

enter image description here

I've tried the Gallery authrization status not working , also this :

   if PHPhotoLibrary.authorizationStatus() == PHAuthorizationStatus.authorized {
            print("[CHECK] ok")
        }else{
            print("[CHECK] not ok")
        } 

is there any way to check if user gives access to add photos ?

i am using the below method to save to gallery :

UIImageWriteToSavedPhotosAlbum(image, {
                                }, nil, nil)
Kodr.F
  • 13,932
  • 13
  • 46
  • 91

2 Answers2

1

well there is not such a method can provide if i have access or not until now , but i've found this class and modified it to provide if i have access or not " saved or not "

import UIKit

class ImageSaver: NSObject {
    
    var onSuccess:(()->()) = {}
    var onFail:((Error?)->()) = {_ in }
    
    init(image: UIImage,onSuccess:@escaping (()->()),onFail:@escaping ((Error?)->()) ){
        self.onSuccess = onSuccess
        self.onFail = onFail
        super.init()
        UIImageWriteToSavedPhotosAlbum(image, self, #selector(saveError), nil)
    }

    @objc func saveError(_ image: UIImage, didFinishSavingWithError error: Error?, contextInfo: UnsafeRawPointer) {
        if let error = error {
            onFail(error)
        } else {
            onSuccess()
        }
    }
}

how to use :

ImageSaver(image: image) {
 print("Save completed!")
         } onFail: { _ in  
 print("error: \(error.localizedDescription)")
}
Kodr.F
  • 13,932
  • 13
  • 46
  • 91
1

Since iOS 14 you are able to check if the user gave permission for add photos only:

PHPhotoLibrary.authorizationStatus(for: .addOnly)
Kevin van Mierlo
  • 9,554
  • 5
  • 44
  • 76