skip to Main Content

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)

2

Answers


  1. Chosen as BEST ANSWER

    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)")
    }
    

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

    PHPhotoLibrary.authorizationStatus(for: .addOnly)
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search