我可以在未经许可的情况下访问iOS照片库

Bob*_*els 1 camera plist ios swift

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

Hit*_*esh 5

按照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)
    }))
}
Run Code Online (Sandbox Code Playgroud)

请参考参考。

注意:如果您不向用户询问“照片”权限,则增加苹果审阅者团队拒绝您的应用程序的机会。