iOS監聽通訊錄改變(應用每次打開都會收到被關閉期間的改變)

下個周要做關于通訊錄這一塊的一些應用,這塊有一個非常重要的一點,通訊錄變化之后重新上傳通訊錄到服務端.

  • 一般這么做
//監聽通訊錄變化
    void addressBookChanged(ABAddressBookRef addressBook, CFDictionaryRef info, void *context)
    {
        // 比如上傳
    }
    - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
        // Override point for customization after application launch.
        
        ABAddressBookRef addresBook = ABAddressBookCreateWithOptions(NULL, NULL);
        ABAddressBookRegisterExternalChangeCallback(addresBook, addressBookChanged, (__bridge void *)(self.viewcontrller));
    }

順帶看看說明:

// Register an external change callback if you want to be informed of changes to the
// shared Address Book database by other instances or processes. The callback will be
// invoked by CFRunLoop on the thread where it was registered. The ABAddressBook does
// not take any action by default to flush or synchronize cached state with the database.
// If you want to ensure that you don't see any stale values, use ABAddressBookRevert().
// The info argument may eventually contain information describing the change. Currently
// it will always be NULL.

  • iOS9以后引入新的聯系人框架,可以這樣用

  [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(addressBookDidChange:) name:CNContactStoreDidChangeNotification object:nil];

-(void)addressBookDidChange:(NSNotification*)notification{
        // 比如上傳
    }
最后編輯于
?著作權歸作者所有,轉載或內容合作請聯系作者
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發布,文章內容僅代表作者本人觀點,簡書系信息發布平臺,僅提供信息存儲服務。

推薦閱讀更多精彩內容