当我使用 CNContactPickerViewController
选择没有关联号码的联系人时,永远不会调用此委托(delegate)方法。
/*!
* @abstract Invoked when the picker is closed.
* @discussion The picker will be dismissed automatically after a contact or property is picked.
*/
optional public func contactPickerDidCancel(picker: CNContactPickerViewController)
如果我选择一个有号码的联系人,就会调用该号码。然而,从方法文档看来,无论如何都应该调用它。
我的问题是,如果用户选择没有号码的联系人,我需要提供一个UIAlertController
。但是,只有在 CNContactPickerViewController
被解除后我才能执行此操作。
通过在 viewDidAppear
中使用一些逻辑,我可能会变得非常黑客,但似乎应该有一种更干净的方法。
剩下的唯一委托(delegate)方法是:
/*!
* @abstract Singular delegate methods.
* @discussion These delegate methods will be invoked when the user selects a single contact or property.
*/
optional public func contactPicker(picker: CNContactPickerViewController, didSelectContact contact: CNContact)
optional public func contactPicker(picker: CNContactPickerViewController, didSelectContactProperty contactProperty: CNContactProperty)
/*!
* @abstract Plural delegate methods.
* @discussion These delegate methods will be invoked when the user is done selecting multiple contacts or properties.
* Implementing one of these methods will configure the picker for multi-selection.
*/
optional public func contactPicker(picker: CNContactPickerViewController, didSelectContacts contacts: [CNContact])
optional public func contactPicker(picker: CNContactPickerViewController, didSelectContactProperties contactProperties: [CNContactProperty])
这无助于确定 CNContactPickerViewController
何时实际离开屏幕。
(Xcode8/swift2.3/iOS10)
最佳答案
您可以弹出这样的警报。您还可以添加一个“重试”按钮并重新启动选择器。
func contactPicker(_ picker: CNContactPickerViewController, didSelect contact: CNContact) {
let name = CNContactFormatter.string(from: contact, style: .fullName)
let phones = contact.phoneNumbers
if phones.count == 0 {
let alertController = UIAlertController(title: "Error", message: "\(name) has no phone numbers", preferredStyle: .alert)
alertController.addAction(UIAlertAction(title: "Ok", style: .default) { (action) in })
picker.dismiss(animated: false){
self.present(alertController, animated: true) {}
}
}
//Do stuff here
}
关于ios - CNContactPickerDelegate 的 contactPickerDidCancel 从未调用过,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40245760/