1

Trying to update UIImageView with image from photo library/camera. Seems like some error relating to permission accessing the camera/photo library

Full error: discovery] errors encountered while discovering extensions: Error Domain=PlugInKit Code=13 "query cancelled" UserInfo={NSLocalizedDescription=query cancelled}

import Photos
import UIKit
import MapKit

class detailViewController: UIViewController, UIImagePickerControllerDelegate, UINavigationControllerDelegate {

var selectedName: String?
var selectedImage: UIImage?
var selectedText: String?

@IBOutlet weak var cityName: UILabel!
@IBOutlet weak var cityImage: UIImageView!
@IBOutlet weak var cText: UILabel!

@IBOutlet weak var map: MKMapView!
@IBOutlet weak var lat: UILabel!
@IBOutlet weak var lon: UILabel!

@IBOutlet weak var searchText: UITextField!

@IBOutlet weak var img_view: UIImageView!

let imagePickerController = UIImagePickerController()

override func viewDidLoad() {
    super.viewDidLoad()
    self.cityName.text = selectedName!
    self.cityImage.image = selectedImage!
    self.cText.text = selectedText!

    imagePickerController.delegate = self

}
var picker = UIImagePickerController()

@IBAction func pickButton(_ sender: UIButton) {
checkPermission()
    let alert = UIAlertController(title: nil, message: nil, preferredStyle: .actionSheet)
    alert.addAction(UIAlertAction(title: "Take Photo", style: .default, handler: { _ in
        self.camera()
    }))

    alert.addAction(UIAlertAction(title: "Select Photo", style: .default, handler: { _ in
        self.gallery()
    }))

    alert.addAction(UIAlertAction.init(title: "Cancel", style: .cancel, handler: nil))

    self.present(alert, animated: true, completion: nil)
}

func camera(){
    if(UIImagePickerController .isSourceTypeAvailable(UIImagePickerControllerSourceType.camera)){
        picker.sourceType = UIImagePickerControllerSourceType.camera
        picker.allowsEditing = true
        picker.delegate = self
        self.present(picker, animated: true, completion: nil)
    }
    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)
    }
}

func gallery(){
    picker.sourceType = UIImagePickerControllerSourceType.photoLibrary
    picker.allowsEditing = true
    picker.delegate = self
    self.present(picker, animated: true, completion: nil)
}

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 {

                    print("success")
                }
            })
            print("It is not determined until now")
        case .restricted:
            print("User do not have access to photo album.")
        case .denied:
            print("User has denied the permission.")
        }
    }



}
hhxx
  • 39
  • 5

1 Answers1

2

I'm not sure what the PlugInKit is that generates the error, but if it is trying to access any photos/camera it may well have issues as you never actually check the user has permission to access the camera or the photo library.

You have defined the checkPermission method (which unusually you have nested inside your viewDidLoad()) but you never actually use it.

You need to restructure your code to actually check for permissions prior to trying to access Photo resources.

You also need to be aware that PHPhotoLibrary.requestAuthorization() returns immediately. If you already have permission that will be fine, but if you need to be granted permission you will need to manage the asynchronous nature of this. Use the completion handler to control follow-up actions, but note that the block can run on a background thread, so you will need to force any UI work onto the main thread.

flanker
  • 3,840
  • 1
  • 12
  • 20
  • moved the checkPermission function outside of viewDidLoad() and called it in func pickButton. I now get popup asking if allow device access to photo library, so that's good, but still receiving "query cancelled' error – hhxx Nov 24 '19 at 10:09
  • Good start :). But there is now an issue that the code after the `checkPermissions()` will run before the popup is answered and you know whether the permission has been granted. You'd be better checking if permission is granted, if so calling a separate func that show the dialog, and if not calling the requestAuthorization and then calling that separate func in the completion handler if permission is granted. – flanker Nov 24 '19 at 10:22
  • The above all needs fixing, but I don't think that will ultimately resolve your issue. If you'd have searched SO before asking you'd have found this https://stackoverflow.com/questions/44465904/photopicker-discovery-error-error-domain-pluginkit-code-13 which has a lot of info around this error. – flanker Nov 24 '19 at 10:24