5
votes

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 ?

3
Move the camera initialisation code inside the request authorization closure after checking if the status has been granted and see if it crashes.Rikh
@Rikh i try it , still crash , the crash happen in the closure line before i got a statusmazen

3 Answers

1
votes

NSPhotoLibraryUsageDescription should be sufficient if just want to access the user Photo Library and following code of yours will work fine

PHPhotoLibrary.requestAuthorization { (status) in
   // No crash
}

Add following two keys to info.plist if you need read and write permissions

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

Try this:

Go to your plist. At the very top, it should say Information Property List have this selected. Then right click and Add Row type Privacy - Photo Library Additions Usage Description then to the right of that there is a value column that needs text entered by you this is what the user will see. I hope this helps!

0
votes

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