代码之家  ›  专栏  ›  技术社区  ›  Crashalot

在iOS上选择/编辑联系人属性时未调用CNContactViewControllerDelegate

  •  0
  • Crashalot  · 技术社区  · 6 年前

    代表 CNContactviewController 在编辑或选择属性时不调用。

    编辑新联系人时 contactViewController(_ viewController: CNContactViewController, shouldPerformDefaultActionFor property: CNContactProperty )函数应该被调用,但它不是。

    当用户编辑/选择联系人属性时,如何获得通知?

    复制步骤:

    1. 复制下面的视图控制器。
    2. 编辑/选择联系人 财产。

    预期行为:

    每次编辑/选择属性时都会打印“yo”。

    实际行为:

    没有什么。

    import Foundation
    import Contacts
    import ContactsUI
    
    
    class ContactViewController: UIViewController, CNContactViewControllerDelegate {
    
    
        override func viewDidLoad() {
            super.viewDidLoad()
    
            createContact()
        }
    
    
        func createContact() {
            let contactController = CNContactViewController(forNewContact: nil)
    
            contactController.delegate = self
            contactController.allowsEditing = true
            contactController.allowsActions = true
            contactController.displayedPropertyKeys = [CNContactPostalAddressesKey, CNContactPhoneNumbersKey, CNContactGivenNameKey]
    
            contactController.view.layoutIfNeeded()
    
            present(contactController, animated:true)
        }
    
    
        // =============================================================================================================
        // MARK: CNContactViewControllerDelegate Functions
        // =============================================================================================================
        func contactViewController(_ viewController: CNContactViewController, didCompleteWith contact: CNContact?) {
            viewController.dismiss(animated: true, completion: nil)
            print("hi")
        }
    
    
        func contactViewController(_ viewController: CNContactViewController, shouldPerformDefaultActionFor property: CNContactProperty) -> Bool {
            print("yo")
            return true
        }
    
    
        // =============================================================================================================
        // MARK: UIViewController Functions
        // =============================================================================================================
        override var prefersStatusBarHidden: Bool {
            return true
        }
    
    
        override func didReceiveMemoryWarning() {
            super.didReceiveMemoryWarning()
        }
    }
    
    1 回复  |  直到 6 年前
        1
  •  4
  •   matt    6 年前

    制作CNContactViewController有三个初始值设定项:

    • init(for:)
    • 新联系人: init(forNewContact:)
    • 未知联系人: init(forUnknownContact:)

    第一个和第三个表单调用委托方法 contactViewController(_:shouldPerformDefaultActionFor:)

    contactViewController(_:didCompleteWith:) ,此时新联系人已保存到数据库中。

    预期行为:每次编辑/选择属性时都会打印“yo”。

    当用户编辑/选择联系人属性时,如何获得通知?

    当您使用像Cocoa这样的框架时,您无法满足任何期望。您的期望需要基于框架的实际功能。你可以 希望 CNContactViewController及其代理消息如您所述工作正常,这可能会对苹果提出非常好的增强请求。但事实并非如此 ,所以期待它这样做对你没有任何好处。