2016-05-31 49 views
1

我试过下面的代码,但没有运气。Swift iOS 8+中的通讯簿更改通知

if let addressBook = ABAddressBookCreateWithOptions(nil, nil) { 

    let ref = addressBook 
    let callback: @convention(c) (addressBookRef: ABAddressBookRef!, info: CFDictionaryRef!, context: UnsafeMutablePointer<Void>) -> Void = { 
     (addressBookRef, info, context) in 

     print("Address book editr ") 
    } 

    let addressBookChangeCallback = unsafeBitCast(callback, ABExternalChangeCallback.self) 
    ABAddressBookRegisterExternalChangeCallback(ref, addressBookChangeCallback, nil) 
} 
+0

你是什么万要实现? –

+0

我基本上想要在地址簿中插入/更新/删除联系人的触发器 – Arul

+0

您需要提供更多代码并详细说明您的问题。 –

回答

0

您可以添加NSNotifications以查找任何地址簿更改。要添加一个NSNotification你应该使用: -

NSNotificationCenter.defaultCenter().addObserver(self, selector: "addressBookDidChange:", name: CNContactStoreDidChangeNotification, object: nil) 

而对于去除NSNotification: -

NSNotificationCenter.defaultCenter().removeObserver(self, name: CNContactStoreDidChangeNotification, object: nil) 

最后加一个功能时的地址簿的变化: -

@objc func addressBookDidChange(notification: NSNotification){ 
    //Something You want to do 
} 
0

最新的API在迅速是:

NotificationCenter.default.addObserver(
      self, selector: #selector(addressBookDidChange), name: .CNContactStoreDidChange, object: nil)