简体   繁体   中英

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.

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:

<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 :

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

Here is copy-pasted fragment from rickster's post .

in iOS 11, UIImagePickerController runs as a separate process from your app. 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. 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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