Kudos to @GxocT for the the great workaround! Helped my users immensely.
But I wanted to share my code based on @GxocT solution hoping it will help others in this scenario.
I needed my CNContactViewControllerDelegate
contactViewController(_:didCompleteWith:)
to be called on cancel (as well as done).
Also my code was not in a UIViewController
so there is no self.navigationController
I also dont like using force unwraps when I can help it. I have been bitten in the past so I chained if let
s in the setup
Here's what I did:
Extend CNContactViewController
and place the swizzle function in
there.
In my case in the swizzle function just call the
CNContactViewControllerDelegate
delegate
contactViewController(_:didCompleteWith:)
with self
and
self.contact
object from the contact controller
In the setup code, make sure the swizzleMethod call to
class_getInstanceMethod
specifies the CNContactViewController
class instead of self
And the Swift code:
class MyClass: CNContactViewControllerDelegate {
override func viewDidLoad() {
super.viewDidLoad()
self.changeImplementation()
}
func changeCancelImplementation() {
let originalSelector = Selector(("editCancel:"))
let swizzledSelector = #selector(CNContactViewController.cancelHack)
if let originalMethod = class_getInstanceMethod(object_getClass(CNContactViewController()), originalSelector),
let swizzledMethod = class_getInstanceMethod(object_getClass(CNContactViewController()), swizzledSelector) {
method_exchangeImplementations(originalMethod, swizzledMethod)
}
}
func contactViewController(_ viewController: CNContactViewController, didCompleteWith contact: CNContact?) {
// dismiss the contacts controller as usual
viewController.dismiss(animated: true, completion: nil)
// do other stuff when your contact is canceled or saved
...
}
}
extension CNContactViewController {
@objc func cancelHack() {
self.delegate?.contactViewController?(self, didCompleteWith: self.contact)
}
}
The keyboard still shows momentarily but drops just after the Contacts controller dismisses.
Lets hope apple fixes this
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…