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

4

7

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 ?

Hedelman answered 14/11, 2018 at 19:27 Comment(2)
Move the camera initialisation code inside the request authorization closure after checking if the status has been granted and see if it crashes.Keare
@Keare i try it , still crash , the crash happen in the closure line before i got a statusHedelman
H
0

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

Hedelman answered 15/11, 2018 at 7:58 Comment(0)
G
1

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!

Georama answered 14/11, 2018 at 20:33 Comment(0)
H
1

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>
Hendrix answered 14/11, 2018 at 21:5 Comment(0)
H
0

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

Hedelman answered 15/11, 2018 at 7:58 Comment(0)
K
0

First Add Privacy - Photo Library Additions Usage Description to your info.plist enter image description here

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

Kitchens answered 6/1, 2024 at 18:30 Comment(0)

© 2022 - 2025 — McMap. All rights reserved.