我可以在不请求许可的情况下访问 iOS 照片库

I can access iOS Photo Library without requesting permission

我正在尝试访问我正在编写的新应用程序的照片库。我有一个 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)
}

根据 UIImagePickerController 行为,它从不为 Photos 访问权限的用户提供对话。 UIImagePickerController 只请求 Camera 权限。

您必须手动向用户请求 Photos permission。通过使用以下代码,您可以向用户请求 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.

重要提示:

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.

从 iOS 11 开始,UIImagePickerController 是 运行 远程在一个单独的进程上。 因此,您的应用程序不需要标准的隐私授权来访问照片库,它只针对用户在 imagePicker 中选择的任何资产获得只读访问权限。

要将新资产添加到照片库中,您需要 NSPhotoLibraryAddUsageDescription 在您的 Info.plist - forum thread