ios - CNContactPickerDelegate 的 contactPickerDidCancel 从未调用过

标签 ios swift

当我使用 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/

相关文章:

javascript - 如何检查ios是否正在使用隐私浏览

ios - 当内存消耗攀升但 Leaks 未检测到泄漏时,如何处理 iOS 中的泄漏?

ios - 设置 UISlider 的最小值和最大值时出错

ios - 如何使用金刚鹦鹉从 svg 图像创建一个大的 jpeg 图像?

ios - InputAccessoryView 未显示 - Swift 3 Xcode 8.3 beta( Non-Storyboard )

ios - 自动调整按钮大小以匹配图形设计师在 Swift/Xcode 中的意图的好方法?

ios - 在 iOS 上使用通用 BLE 信标?

ios - 更改 tableView 中所有单元格的所有对象的 bool 状态

ios - 无法在 Xcode 中构建,错误为 "[method] has been explicitly marked unavailable"

swift - 如何在 Swift 中调用 HTTP Get 并保存到变量中?