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:
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:
Using it...
What do you suggest?
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?
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.
For more information, you can check out https://docs.swift.org/swift-book/LanguageGuide/AutomaticReferenceCounting.html.