Bob Samuels
Bob Samuels

Reputation: 425

I can access iOS Photo Library without requesting permission

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

Upvotes: 4

Views: 8954

Answers (3)

Marwan Alqadi
Marwan Alqadi

Reputation: 855

yes you can access photos by PhotosUI library without permission

import PhotosUI

var config = PHPickerConfiguration()
    config.selectionLimit = 4
    config.filter = .images
    let vc = PHPickerViewController(configuration: config)
    vc.delegate = self
    self.present(vc, animated: true)

extension UIViewController: PHPickerViewControllerDelegate {

func picker(_ picker: PHPickerViewController, didFinishPicking results: [PHPickerResult]) {
     
 var images = [UIImage]()
    for image in results {
        if image.itemProvider.canLoadObject(ofClass: UIImage.self)  {
            image.itemProvider.loadObject(ofClass: UIImage.self) { (newImage, error) in
                if let error = error {
                    print(error.localizedDescription)
                } else {
                    self.images.append(newImage as! UIImage)
                }
            }
        } else {
            print("Loaded Assest is not a Image")
        }
    }
    picker.dismiss(animated: true)
}

}

Upvotes: 0

Hitesh Surani
Hitesh Surani

Reputation: 13537

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.

Upvotes: 8

Pratik Sodha
Pratik Sodha

Reputation: 3727

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

Upvotes: 3

Related Questions