简体   繁体   English

UIImagePickerController和info.plist问题

[英]UIImagePickerController and info.plist issue

In my project I tried to add two things: 在我的项目中,我尝试添加两件事:

  1. A camera source for a person's avatar. 一个人的化身的相机源。
  2. add permission for the camera and photo library access in the info.plist file. info.plist文件中添加相机和照片库访问权限。

But when I tried to test my app in a real device the app doesn't show any permission, and when I chose the camera the app exit without error message in the log console? 但是,当我尝试在真实设备上测试我的应用程序时,该应用程序未显示任何权限,而当我选择摄像头时,该应用程序退出却没有在日志控制台中显示错误消息?

For the info.plist I use this two line: 对于info.plist,我使用以下两行:

<key>NSCameraUsageDescription</key>
<string>$(PRODUCT_NAME) camera use</string>
<key>NSPhotoLibraryUsageDescription</key>
<string>$(PRODUCT_NAME) photo use</string>

And add a photo I use this code: 并添加一张照片,我使用以下代码:

@objc func addNewPerson(){
    let picker = UIImagePickerController()

    let ac = UIAlertController(title: "Choose image", message: nil, preferredStyle: .actionSheet)

    let camera = UIAlertAction(title: "Camera", style: .default){
        [weak self] _ in
        if UIImagePickerController.isSourceTypeAvailable(.camera) {
            picker.allowsEditing = false
            picker.delegate = self
            picker.sourceType = .camera
            self?.present(picker,animated: true)
        }else{
            let alert  = UIAlertController(title: "Warning", message: "You don't have camera", preferredStyle: .alert)
            alert.addAction(UIAlertAction(title: "OK", style: .default, handler: nil))
            self?.present(alert, animated: true, completion: nil)
        }
    }
    ac.addAction(camera)

    let galerie = UIAlertAction(title: "Photo library", style: .default){
        [weak self] _ in
            picker.allowsEditing = true
            picker.delegate = self
        self?.present(picker,animated: true)
    }
    ac.addAction(galerie)

    ac.addAction(UIAlertAction(title: "Cancel", style: .cancel))

    present(ac,animated: true)   
}

Thank you. 谢谢。

in your addNewPerson() method before showing the alert you should check if the user already granted access to his Camera you can do that by : 在显示警报之前,在addNewPerson()方法中,应检查用户是否已被授予访问其Camera的权限,您可以通过以下方式进行操作:

if AVCaptureDevice.AVCaptureDevice.authorizationStatus(AVMediaTypeVideo) ==  AVAuthorizationStatus.Authorized {
    // Already Authorized
} else {
    AVCaptureDevice. requestAccess(AVMediaTypeVideo, completionHandler: { (granted: Bool) -> Void in
       if granted == true {
           // User granted
       } else {
           // User rejected
       }
   })
}

and before that make sure you added 在此之前,请确保您添加了 在此处输入图片说明

Just to mention, in iOS 11 camera access is not required to use the UIImagePicker 只需提一下,在iOS 11中不需要使用UIImagePicker进行摄像头访问

Here is copy-pasted fragment from rickster's post . 这是里克斯特的帖子上贴有粘贴内容的片段。

in iOS 11, UIImagePickerController runs as a separate process from your app. 在iOS 11中,UIImagePickerController与应用程序分开运行。 That means: 这意味着:

Your app can't see the user's whole Photos library — it gets read-only access just for whichever asset(s) the user chooses in the image picker. 您的应用看不到用户的整个照片库-它仅对用户在图像选择器中选择的任何资产具有只读访问权限。 Because of (1), your app doesn't need the standard privacy authorization for Photos library access. 由于(1),您的应用不需要标准的隐私授权即可访问照片库。 The user explicitly chooses a specific asset (or multiple) for use in your app, which means the user is granting your app permission to read the asset(s) in question. 用户明确选择了一个(或多个)特定资产供您的应用程序使用,这意味着该用户已授予您的应用程序读取相关资产的权限。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM