4
votes

I am trying to access the photo library for a new app that I am writing. I have an actionSheet that allows the user to choose between their camera and their photo library. When opening the camera, it asks the user for permission but when opening the photo library it does not. Despite this, it still brings the user to their photo library. I specifically referenced it in my info.plist, but there is still no difference. Any help? My code:

@IBAction func allowAccessToPhotos(_ sender: Any) {

    let imagePickerController = UIImagePickerController()
    imagePickerController.delegate = self

    let actionSheet = UIAlertController(title: "Photo Source", message: "Choose a Source", preferredStyle: .actionSheet)

    actionSheet.addAction(UIAlertAction(title: "Photo Library", style: .default, handler: { (action:UIAlertAction) in imagePickerController.sourceType = .photoLibrary
        self.present(imagePickerController, animated: true, completion: nil)

    }))

    actionSheet.addAction(UIAlertAction(title: "Camera", style: .default, handler: { (action:UIAlertAction) in imagePickerController.sourceType = .camera
        self.present(imagePickerController, animated: true, completion: nil)

    }))

    actionSheet.addAction(UIAlertAction(title: "Cancel", style: .cancel, handler: nil))

    self.present(actionSheet, animated: true, completion: nil)
}
2
What have you added in your Info.plist ?Mayur Karmur
@MayurKarmur I have added 'Privacy - Camera Usage Description' and 'Privacy - Photo Library Usage Description'Bob Samuels
This is right keys, but not asking for permission, it's weird.Mayur Karmur
@BobSamuels have you installed your app before and given permission if so you might like to check permissions in apps settingJarvis The Avenger
@BobSamuels What is happening is iOS is saving the permission granted to your app mapped to the bundle ID if the app is deleted this data persists for 24 hours, this avoids re-prompting the user if they reinstall the app (perhaps after mistakingly deleting an app). This also happens for Push Notification prompts.Jarvis The Avenger

2 Answers

8
votes

As per UIImagePickerController behaviour, It never gives dialogue to the user for Photos access. UIImagePickerController only ask for the Camera permission.

You have to ask for Photos permission manually to the user. By using the below code you can ask the user for Photos permission.

import Photos
    
@IBAction func allowAccessToPhotos(_ sender: Any) {
    
    let imagePickerController = UIImagePickerController()
    imagePickerController.delegate = self
    let actionSheet = UIAlertController(title: "Photo Source", message: "Choose a Source", preferredStyle: .actionSheet)
    actionSheet.addAction(UIAlertAction(title: "Photo Library", style: .default, handler: { (action:UIAlertAction) in imagePickerController.sourceType = .photoLibrary
        let photoAuthorizationStatus = PHPhotoLibrary.authorizationStatus()
        switch photoAuthorizationStatus {
        case .authorized:
            self.present(imagePickerController, animated: true, completion: nil)
        case .notDetermined:
            PHPhotoLibrary.requestAuthorization({
                (newStatus) in
                DispatchQueue.main.async {
                    if newStatus ==  PHAuthorizationStatus.authorized {
                        self.present(imagePickerController, animated: true, completion: nil)
                    }else{
                        print("User denied")
                    }
                }})
            break
        case .restricted:
            print("restricted")
            break
        case .denied:
            print("denied")
            break
        }}))
    
    actionSheet.addAction(UIAlertAction(title: "Camera", style: .default, handler: { (action:UIAlertAction) in imagePickerController.sourceType = .camera
        self.present(imagePickerController, animated: true, completion: nil)
    }))
}

Please refer reference.

Important Note:

If you're not asking for Photos permission to the user then It will cause rejection by the apple team. It depends on your luck, Sometimes the apple team ignore it and sometimes reject our app.

2
votes

From iOS 11, UIImagePickerController is running remotely on a separate process. So, your app doesn't need the standard privacy authorization for Photos library access, it gets read-only access just for whichever asset(s) the user chooses in the imagePicker.

To add new asset into photo-library you need NSPhotoLibraryAddUsageDescription in your Info.plist - forum thread