PHPicker delegate error: PHPickerViewControllerDelegate doesn't respond to picker:didFinishPicking

In WWDC20 was introduced PHPicker, the replacement for UIImagePickerController.

I have a wrapper class witch handles all the configuration and presentation of image picker controller, now I'm replacing the implementation to support iOS14.

Even if I set the delegate to be self I get the error:

Code Block
[Picker] PHPickerViewControllerDelegate doesn't respond to picker:didFinishPicking:


I think it checks on the parent view controller, that indeed it's not implementing the delegate methods but the wrapper does.

Here is my example code:

Code Block swift
import Foundation
import PhotosUI
class myPicker: PHPickerViewControllerDelegate{
func openFrom(parent:UIViewController!) {
var config:PHPickerConfiguration! = PHPickerConfiguration()
config.selectionLimit = 1
config.filter = nil
let pickerViewController:PHPickerViewController! = PHPickerViewController(configuration:config)
pickerViewController.delegate = self //<---
parent.present(pickerViewController, animated:true, completion:nil)
}
func picker(_ picker: PHPickerViewController, didFinishPicking results: [PHPickerResult]) {
picker.dismiss(animated: true, completion:nil)
for result:PHPickerResult in results {
let itemProvider:NSItemProvider = result.itemProvider
print(itemProvider)
}
// ...do something with images...
}
}


Using it...
Code Block
override func viewDidAppear(_ animated: Bool) {
super.viewDidAppear(animated)
let mypicker = myPicker()
mypicker.openFrom(parent: self)
}


What do you suggest?
Answered by Engineer in 619879022
You need to hold a strong reference to your delegate object (in this case, mypicker). pickerViewController.delegate is weakly referenced to resolve strong reference cycles. In your code example, mypicker will be released when viewDidAppear is finished. So PHPickerViewController will not be able to call the delegate method when an asset is selected by the user.

For more information, you can check out https://docs.swift.org/swift-book/LanguageGuide/AutomaticReferenceCounting.html.
Accepted Answer
You need to hold a strong reference to your delegate object (in this case, mypicker). pickerViewController.delegate is weakly referenced to resolve strong reference cycles. In your code example, mypicker will be released when viewDidAppear is finished. So PHPickerViewController will not be able to call the delegate method when an asset is selected by the user.

For more information, you can check out https://docs.swift.org/swift-book/LanguageGuide/AutomaticReferenceCounting.html.
You're right!

Thanks! ...Tired and dumb question...
PHPicker delegate error: PHPickerViewControllerDelegate doesn't respond to picker:didFinishPicking
 
 
Q