2016-05-31 95 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)