Reputation: 448
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 ?
Upvotes: 7
Views: 14211
Reputation: 1
First Add Privacy - Photo Library Additions Usage Description to your info.plist
Then you can used bellow code
import SwiftUI
import Photos
struct ContentView: View {
@State private var showingAlert = false
@State private var authorizationStatus = PHAuthorizationStatus.notDetermined
var body: some View {
VStack {
Text("\(authorizationStatus.rawValue)")
Button(action: {
PHPhotoLibrary.requestAuthorization(for: .addOnly) { status in
authorizationStatus = status
if status == .denied {
showingAlert = true
}
}
}) {
Text("Hello, world!")
}
.alert(isPresented: $showingAlert) {
Alert(
title: Text("Permission Denied"),
message: Text("You have denied access to the photo library. Please enable access in your settings if you want to use this feature."),
dismissButton: .default(Text("OK")) {
// Action to open the app settings
if let settingsURL = URL(string: UIApplication.openSettingsURLString),
UIApplication.shared.canOpenURL(settingsURL) {
UIApplication.shared.open(settingsURL)
}
}
)
}
}
.padding()
}
}
#Preview {
ContentView()
}
Upvotes: 0
Reputation: 448
i just add the key NSPhotoLibraryUsageDescription
in Project/info/Custom IOS Target Properties . instead of info.plist
and now it's working fine
Upvotes: 0
Reputation: 2043
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>
Upvotes: 1
Reputation: 21
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!
Upvotes: 1