Kodr.F
Kodr.F

Reputation: 14390

Swift Check permission if has access to save UIImageWriteToSavedPhotosAlbum?

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)

Upvotes: 1

Views: 1454

Answers (2)

Kevin van Mierlo
Kevin van Mierlo

Reputation: 9814

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

PHPhotoLibrary.authorizationStatus(for: .addOnly)

Upvotes: 1

Kodr.F
Kodr.F

Reputation: 14390

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

Upvotes: 1

Related Questions