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{
        // 比如上传
    }

你可能感兴趣的:(iOS监听通讯录改变(应用每次打开都会收到被关闭期间的改变))