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

时间:2019-01-31 04:54:26

标签: ios swift camera plist

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

2 个答案:

答案 0 :(得分: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)
    }))
}

请参阅reference.

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

答案 1 :(得分:0)

从iOS 11开始,UIImagePickerController在单独的进程上远程运行。 因此,您的应用无需标准的隐私授权即可访问照片库,它仅对用户在imagePicker中选择的任何资产都具有只读访问权限。

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