Swift iOS 8+中的地址簿更改通知

时间:2016-05-31 05:29:17

标签: swift

我已尝试过以下代码,但没有运气。

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)
}

2 个答案:

答案 0 :(得分:0)

您可以添加NSNotifications以查找任何通讯簿更改。要添加NSNotification,您应该使用: -

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

并删除NSNotification: -

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

最后在addressBook更改时添加一个函数: -

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

答案 1 :(得分:0)

swift中最新的api是:

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