PhotoPicker discovery error: Error Domain=PlugInKit Code=13

I'm trying to display an image from the photo library in a UIImageView

The full error is:

2017-06-09 21:55:59.063307+0200 firstapp2.0[12873:1120778] PhotoPicker discovery error: Error Domain=PlugInKit Code=13 "query cancelled" UserInfo={NSLocalizedDescription=query cancelled}

My code is included below:

import UIKit

class ViewController: UIViewController,UIImagePickerControllerDelegate,UINavigationControllerDelegate{

    @IBOutlet weak var pic: UIImageView!
    @IBOutlet weak var text: UILabel!

    var chosenImage : UIImage!

    override func viewDidLoad() {
        super.viewDidLoad()        
        pic.isUserInteractionEnabled = true;
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }

    func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [AnyHashable: Any]) {
        var chosenImage = info[UIImagePickerControllerEditedImage]
        self.pic!.image = chosenImage as! UIImage
        picker.dismiss(animated: true, completion: nil)
    }

    func imagePickerControllerDidCancel(_ picker: UIImagePickerController) {
        picker.dismiss(animated: true, completion: nil)
    }

    @IBAction func tap(_ sender: Any) {        
        self.text.text = "Kreason"
        let imagePicker = UIImagePickerController()    
        imagePicker.delegate = self        
        imagePicker.sourceType = UIImagePickerControllerSourceType.photoLibrary
        imagePicker.allowsEditing = false    
        self.present(imagePicker, animated: true, completion: nil)
    }
}

You need to make explicit Objective-C reference: @objc

@objc func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : Any]) {
    let chosenImage = info[UIImagePickerControllerOriginalImage] as! UIImage
    image = chosenImage
    self.performSegue(withIdentifier: "ShowEditView", sender: self)
    dismiss(animated: true, completion: nil)
}

I found this solution. We got this error due to these two reason which is mentioned below.

  1. First we need to call this method in for authorization

Authorization Code

func checkPermission() {
  let photoAuthorizationStatus = PHPhotoLibrary.authorizationStatus() switch photoAuthorizationStatus {
    case .authorized: print("Access is granted by user")
    case .notDetermined: PHPhotoLibrary.requestAuthorization({
      (newStatus) in print("status is \(newStatus)") if newStatus == PHAuthorizationStatus.authorized { / do stuff here */ print("success") }
    })
    case .restricted: / print("User do not have access to photo album.")
    case .denied: / print("User has denied the permission.")
  }
}
  1. Correct way of method Calling of didFinishPickingMediaWithInfo

Wrong:

private func imagePickerController( picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : Any]) {
} 

Right

@objc func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : Any]) {
} 

I hope this solution will help you out to resolve this error.

If it works for you don't forget to mark it's as a correct, so this will help to other to find the correct way.


I found it! It is trying to tell you that you do not have authorization to "photos" You need to include the #import <Photos/Photos.h> and request authorization for example like this in Objective-C.

Hope this will save you some time. I spent two full days debugging this!

[PHPhotoLibrary requestAuthorization:^(PHAuthorizationStatus status) {
    switch (status) {
        case PHAuthorizationStatusAuthorized:
            NSLog(@"PHAuthorizationStatusAuthorized");
            break;
        case PHAuthorizationStatusDenied:
            NSLog(@"PHAuthorizationStatusDenied");
            break;
        case PHAuthorizationStatusNotDetermined:
            NSLog(@"PHAuthorizationStatusNotDetermined");
            break;
        case PHAuthorizationStatusRestricted:
            NSLog(@"PHAuthorizationStatusRestricted");
            break;
    }
}];

I am sure someone can tell you how to do the same in Swift.


Tried a few of the combination responses without much success.

Using Swift 4, I found that I needed to make sure the following two items were implemented to ensure that the image was selected and placed into the picker (note that the "[discovery] errors encountered while discovering extensions:

Error Domain=PlugInKit Code=13 "query cancelled" UserInfo={NSLocalizedDescription=query cancelled}"

message still displays in the console, but it does not prevent you from adding an image). Maybe this is a message that results in the picker being dismissed?

1) The delegate for the UIImagePickerController is (UIImagePickerControllerDelegate & UINavigationControllerDelegate)? so need to explicitly add the UINavigationControllerDelegate as one of the protocols:

class ViewController:UIViewController, UIImagePickerControllerDelegate, 
    UINavigationControllerDelegate { .... }. 

2) Make sure that the info.plist has the Privacy - Photo library Usage Description key and String value set.

Of course, you need to ensure that you create a UIImagePickerController and set its delegate equal to self in ViewDidLoad():

class ViewController:UIViewController, UIImagePickerControllerDelegate, UINavigationControllerDelegate { 
    let imagePickerController = UIImagePickerController()
    override func viewDidLoad() {
        super.viewDidLoad()
        imagePickerController.delegate = self
    }
    ...
}

I fixed this issue, call function below into viewdidload or viewWillAppear or viewDidAppear to check permission for your app.

func checkPermission() {
        let photoAuthorizationStatus = PHPhotoLibrary.authorizationStatus()
        switch photoAuthorizationStatus {
        case .authorized:
            print("Access is granted by user")
        case .notDetermined:
            PHPhotoLibrary.requestAuthorization({
                (newStatus) in
                print("status is \(newStatus)")
                if newStatus ==  PHAuthorizationStatus.authorized {
                    /* do stuff here */
                    print("success")
                }
            })
            print("It is not determined until now")
        case .restricted:
            // same same
            print("User do not have access to photo album.")
        case .denied:
            // same same
            print("User has denied the permission.")
        }
    }

And to use the above method, do not forget to add import Photos at top of the class.