ABAddressBook-如何查找特定联系人是否被修改或从iPhone中删除';谁的通讯录?

ABAddressBook-如何查找特定联系人是否被修改或从iPhone中删除';谁的通讯录?,iphone,contacts,abaddressbook,abrecord,abperson,Iphone,Contacts,Abaddressbook,Abrecord,Abperson,我正在开发一个聊天应用程序。我需要为其保存联系人。因此,我正在将ABAddressBook中的所有联系人保存在我的核心数据实体中。问题是,我如何知道某个联系人是否被修改或从iPhone的通讯录中删除?,这样我就可以从我的核心数据实体中修改或删除该联系人 if ABRecordGetRecordID(person) can be used as unique key or not 给定的belew是在核心数据中添加联系人的代码 ABAddressBookRef addressBookRef =

我正在开发一个聊天应用程序。我需要为其保存联系人。因此,我正在将ABAddressBook中的所有联系人保存在我的核心数据实体中。问题是,我如何知道某个联系人是否被修改或从iPhone的通讯录中删除?,这样我就可以从我的核心数据实体中修改或删除该联系人

if ABRecordGetRecordID(person) can be used as unique key or not
给定的belew是在核心数据中添加联系人的代码

ABAddressBookRef addressBookRef = ABAddressBookCreateWithOptions(NULL, nil);
CFArrayRef allPeople = ABAddressBookCopyArrayOfAllPeople( addressBookRef );
CFIndex nPeople = ABAddressBookGetPersonCount( addressBookRef );

for ( int i = 0; i < nPeople; i++ )
{
    ABRecordRef person = CFArrayGetValueAtIndex( allPeople, i );
 } 

 NSString* firstName = (__bridge_transfer NSString*)ABRecordCopyValue(person,kABPersonFirstNameProperty);


    if ([firstName hasPrefix:@"Protected by True"])
    {
        continue;
    }
    else if([firstName hasPrefix:@"Identified As Spam"])
    {
        continue;
    }


    NSString* lastName  = (__bridge_transfer NSString*)ABRecordCopyValue(person,kABPersonLastNameProperty);

    NSString *userEmail = nil;
    ABMultiValueRef emailData = ABRecordCopyValue(person, kABPersonEmailProperty);
    if(ABMultiValueGetCount(emailData) > 0) {
        NSLog(@"email is:%@",(__bridge NSString *)ABMultiValueCopyValueAtIndex(emailData, 0));
        userEmail = (__bridge NSString *)ABMultiValueCopyValueAtIndex(emailData, 0);
    }

    NSString* phone = nil;
    ABMultiValueRef phoneNumbers = ABRecordCopyValue(person,kABPersonPhoneProperty);
    if (ABMultiValueGetCount(phoneNumbers) > 0)
    {
        for (int i=0; i<ABMultiValueGetCount(phoneNumbers); i++)
        {
            phone = (__bridge_transfer NSString*)ABMultiValueCopyValueAtIndex(phoneNumbers, i);



            NSString *asciiCharacters = @"0123456789";
            NSCharacterSet *nonAsciiCharacterSet = [[NSCharacterSet characterSetWithCharactersInString:asciiCharacters] invertedSet];





            NSString *stringPhone = [[phone componentsSeparatedByCharactersInSet:nonAsciiCharacterSet] componentsJoinedByString: @""];
            NSString *str = stringPhone;
            stringPhone = [str stringByReplacingOccurrencesOfString:@"^0+"
                                                         withString:@""
                                                            options:NSRegularExpressionSearch
                                                              range:NSMakeRange(0, str.length)];






            NSLog(@"modified %@", stringPhone);

            NSString *phoneLabelLocalized;
            CFStringRef labelStingRef = ABMultiValueCopyLabelAtIndex (phoneNumbers, i);
            phoneLabelLocalized = (__bridge NSString*)ABAddressBookCopyLocalizedLabel(labelStingRef);


            if ([[PersistentStore getMyJID] rangeOfString:stringPhone].location != NSNotFound)
            {
                NSLog(@"my phone  %@",stringPhone);
                continue;
            }

            NSFetchRequest *request = [[NSFetchRequest alloc] init];
                request.entity = [NSEntityDescription entityForName:@"Contact" inManagedObjectContext:appDelegate.managedObjectContext];
                request.predicate = [NSPredicate predicateWithFormat:@"contactNumber = %@", stringPhone];
                NSError *executeFetchError = nil;
                Contact  *contact = [[appDelegate.managedObjectContext executeFetchRequest:request error:&executeFetchError] lastObject];

                if (!contact)
                {
                    contact = (Contact *)[NSEntityDescription insertNewObjectForEntityForName:@"Contact" inManagedObjectContext:appDelegate.managedObjectContext];
                    [contact setContactFirstName:firstName];
                    [contact setContactLastName:lastName];

                    if (firstName && lastName)
                    {
                        [contact setContactFullName:[NSString stringWithFormat:@"%@ %@",firstName,lastName]];
                    }
                    else if(firstName)
                    {
                       [contact setContactFullName:firstName];
                    }
                    else
                    {
                        [contact setContactFullName:lastName];

                    }
                    [contact setContactType:phoneLabelLocalized];
                    [contact setContactDate:[NSDate date]];
                    [contact setContactOriginalNumber:phone];
                    [contact setContactNumber:stringPhone];
                    [contact setContactEmail:userEmail];
                    [arrayContacts addObject:stringPhone];

                }

            }
        }
        CFRelease(phoneNumbers);


        NSError *error;
        if (![appDelegate.managedObjectContext save:&error])
        {
            // This is a serious error saying the record could not be saved.
            // Advise the user to restart the application
        }
    }
ABAddressBookRef addressBookRef=ABAddressBookCreateWithOptions(NULL,nil);
CFArrayRef allPeople=abAddressBookCopyArrayFallPeople(addressBookRef);
CFIndex nPeople=ABAddressBookGetPersonCount(addressBookRef);
for(int i=0;i0){
NSLog(@“email is:%@”,(u bridge NSString*)ABMultiValueCopyValueAtIndex(emailData,0));
userEmail=(uu-bridge-NSString*)ABMultiValueCopyValueAtIndex(emailData,0);
}
NSString*phone=nil;
ABMultiValueRef phoneNumbers=ABRecordCopyValue(person,kABPersonPhoneProperty);
如果(ABMultiValueGetCount(电话号码)>0)
{

for(int i=0;i
ABAddressBookRegisterExternalChangeCallback
将在通讯簿更改时为您提供回调

ABRecordGetRecordID
为您提供通讯簿条目的唯一标识符(适用于设备,而非全局唯一)

这两件事足以让您检测到正在修改和删除的记录。

First Set Callback

ABAddressBookRef callBackAddressBook = ABAddressBookCreate();
ABAddressBookRegisterExternalChangeCallback(callBackAddressBook, AddressBookExternalChangeCallback, self);
当通知发生时,它将调用下面的回调方法

void AddressBookExternalChangeCallback (ABAddressBookRef callBackAddressBook,CFDictionaryRef info,void *context)
{
// use  callBackAddressBook and get information about the changes
}