iosswiftpermissionsswift4

Swift Ask for user’s permission to access the photo library


I'm trying to access the photo library to import a profile image in swift 4.

but i got this error :

This app has crashed because it attempted to access privacy-sensitive data without a usage description. The app's Info.plist must contain an NSPhotoLibraryUsageDescription key with a string value explaining to the user how the app uses this data.

I asked for permission in every single way.

I try :

<key>NSPhotoLibraryUsageDescription</key>
    <string>this permission for profile photo only</string>

and the code :

let imagePicker = UIImagePickerController()
 override func viewDidLoad() {
     super.viewDidLoad()
     imagePicker.delegate = self
}
@objc func importUserImage() {
        print("import driver")

        imagePicker.sourceType = .photoLibrary
        imagePicker.allowsEditing = true
        self.present(imagePicker, animated: true)
    }

I try

<key>NSPhotoLibraryAddUsageDescription</key>
    <string>this permission for profile photo only</string>

I even try to do it in code

import UIKit
import Photos
import UserNotifications

class LoginViewController: UIViewController {

    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view, typically from a nib.
        PHPhotoLibrary.requestAuthorization { (status) in
            // cruch here
        }
     }
}

I deleted the app and reinstall it , still crash

Did i forget something? or what i'm doing wrong ?


Solution

  • i just add the key NSPhotoLibraryUsageDescription in Project/info/Custom IOS Target Properties . instead of info.plist and now it's working fine