我无需请求权限即可访问iOS照片库

bprjcwpo  于 2022-12-15  发布在  iOS
关注(0)|答案(3)|浏览(334)

我正在尝试访问我正在编写的一个新应用程序的照片库。我有一个actionSheet,允许用户在他们的相机和他们的照片库之间进行选择。当打开相机时,它会询问用户的权限,但当打开照片库时,它不会。尽管如此,它仍然会将用户带到他们的照片库。我在我的info.plist中特别引用了它,但还是没有区别。有什么帮助吗?我的代码:

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

qhhrdooz1#

根据UIImagePickerController行为,它不会向用户提供Photos访问对话框。UIImagePickerController仅请求Camera权限。
您必须手动向用户请求Photospermission。通过使用下面的代码,您可以向用户请求Photos权限。

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

请参考reference.

重要提示:

如果你没有向用户请求Photos权限,那么它将导致苹果团队的拒绝。这取决于你的运气,有时苹果团队忽略它,有时拒绝我们的应用程序。

3zwtqj6y

3zwtqj6y2#

从iOS 11开始,UIImagePickerController将在一个单独的进程上远程运行。因此,您的应用不需要标准隐私授权即可访问照片图库,它只对用户在imagePicker中选择的任何资源进行只读访问。
要将新资产添加到照片库中,您需要在信息列表中添加NSPhotoLibraryAddUsageDescription-forum thread

hivapdat

hivapdat3#

是的,您可以通过PhotosUI库访问照片,无需许可

import PhotosUI

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

扩展UI视图控制器:PHPickerView控制器委托{

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

}

相关问题