Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
301 views
in Technique[技术] by (71.8m points)

iphone - Detect what was changed from ABAddressBookRegisterExternalChangeCallback

I am using ABAddressBookRegisterExternalChangeCallback to get the external changes in AddressbookBook of the user. I am using the following code to register the callback:

ABAddressBookRef ntificationaddressbook = ABAddressBookCreate();
ABAddressBookRegisterExternalChangeCallback(ntificationaddressbook, MyAddressBookExternalChangeCallback, self);

and when this callback is called then MyAddressBookExternalChangeCallback is called successfully

void MyAddressBookExternalChangeCallback (ABAddressBookRef ntificationaddressbook,CFDictionaryRef info,void *context)
{
 NSLog(@"Changed Detected......");
}

I have the following questions:

  1. How can i detect which contacts was changed, and which action (ADD,Update,Delete) was performed on that Contact. I need to get the recordID of that contact. Is it Possible ?

Unfortunately if thats not possible then how the apps like viber, tango, watsapp gets the change information ?

  1. I gets the callback method called only when the app is in background if the app is terminated is there any way to get the changes notification.

Please help. Thanks in advance.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

I found the solution to find what was changed in the addressbook. I am not sure if its a precise one, But some how that works.

Create a Callback Notification for Addressbook:-

    ABAddressBookRef ntificationaddressbook = ABAddressBookCreate();
    ABAddressBookRegisterExternalChangeCallback(ntificationaddressbook, MyAddressBookExternalChangeCallback, self);

Than Add the following code when notification Occurs:-

void MyAddressBookExternalChangeCallback (ABAddressBookRef ntificationaddressbook,CFDictionaryRef info,void *context)
{
NSTimeInterval timeStampone = [[NSDate date] timeIntervalSince1970];
NSNumber *timeStamponeobj= [NSNumber numberWithDouble: timeStampone];
NSLog(@"Start Process");
NSUserDefaults *userdefs = [[NSUserDefaults alloc]init];
NSMutableArray *arraytemp = [[NSMutableArray alloc]init];
CFArrayRef peopleRefs = ABAddressBookCopyArrayOfAllPeopleInSource(ntificationaddressbook, kABSourceTypeLocal);
NSMutableArray *changedrecords = [[NSMutableArray alloc]init];
ABAddressBookRevert(ntificationaddressbook);
CFIndex count = CFArrayGetCount(peopleRefs);
for (CFIndex i=0; i < count; i++) {
    ABRecordRef ref = CFArrayGetValueAtIndex(peopleRefs, i);
    NSDate* datemod = ( NSDate *)(ABRecordCopyValue(ref, kABPersonModificationDateProperty));
    NSDate *lastopened = [userdefs valueForKey:@"LastOpenedDate"];
    NSTimeInterval datemodifiedtime =[datemod timeIntervalSince1970];
    NSNumber *modifieddatenumber= [NSNumber numberWithDouble: datemodifiedtime];
    NSTimeInterval lastopeddate = [lastopened timeIntervalSince1970];
    NSNumber *lastopenednumber= [NSNumber numberWithDouble: lastopeddate];
    if ([modifieddatenumber intValue]>[lastopenednumber intValue]) {
        ABRecordRef aSource = CFArrayGetValueAtIndex(peopleRefs,i);
         int recordid = ABRecordGetRecordID(aSource);
        [changedrecords addObject:[NSString stringWithFormat:@"%d",recordid]];
    }
}



NSString *arraystring= [changedrecords componentsJoinedByString:@","];
[userdefs setValue:arraystring forKey:@"ArrayOfChangedPeopleString"];
[userdefs setValue:changedrecords forKey:@"ArrayOfChangedPeople"];


//-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
if ([arraystring isEqualToString:@"(null)"]) {
    arraystring = [arraystring stringByReplacingOccurrencesOfString:@"(null)" withString:@""];
}
NSLog(@"arraychangedstring= %@ and length = %d",arraystring,arraystring.length);

NSLog(@"Must send data to the server here for this changed people array= %@",changedrecords);
if (arraystring.length>2) {
    NSLog(@"inside if");
    ABAddressBookRef addressBook = ABAddressBookCreate();
    CFArrayRef allSources = ABAddressBookCopyArrayOfAllPeople( addressBook );
    [userdefs setValue:@"" forKey:@"ArrayOfChangedPeopleString"];
    [userdefs setValue:@"" forKey:@"ArrayOfChangedPeople"];
    for (int i = 0; i<[changedrecords count]; i++) {
        NSString *recordid= [changedrecords objectAtIndex:i];
        int nPeople = [recordid intValue];
        [IntoochUtil current_function_name:@"Inside getcontactdetails - Start"];

        if(nPeople != 0){
            //        for (CFIndex i = nPeople_start; i < (nPeople_end); i++)
            //        {
            //            NSString *reocrdid = [NSString stringWithFormat: @"Record %ld",i];
            //            NSLog(@"Recordid= %@",reocrdid);

            NSMutableDictionary *dict = [[NSMutableDictionary alloc]init];
            //common field
            NSString *first_name =nil;
            NSString *mid_name = nil ;
            NSString *last_name =nil ;
            NSString *basic_email =nil ;
            NSString *basic_mobile =nil ;
            NSString *other_phone=nil;
            NSString *pager_phone= nil;

            //personal filed
            NSString *Home_email =nil ;
            NSString*home_mobile =nil ;
            NSString*home_address =nil;
            //business filed
            NSString *work_email =nil ;
            NSString *company_name =nil ;
            NSString *job_title =nil ;
            NSString*work_mobile =nil ;
            NSString *iphone = nil;
            NSString *main = nil;
            NSString *work_address =nil ;

            NSString *home_fax, *work_fax, *other_fax =nil;
            NSString *birthday = nil;
            //            NSString *blogs = nil;

            NSString *record_id;
            ABRecordRef aSource = ABAddressBookGetPersonWithRecordID(addressBook, nPeople);
            // Fetch all groups included in the current source

            // Getting Record id from address book
            int recordid = ABRecordGetRecordID(aSource);
            record_id = [NSString stringWithFormat:@"%d",recordid];

            // Getting Names from address book
            //            CFStringRef firstName = ABRecordCopyValue(aSource, kABPersonFirstNameProperty);
            first_name= ABRecordCopyValue(aSource, kABPersonFirstNameProperty);
            //           first_name=[NSString stringWithFormat:@"%@",firstName];
            //           CFRelease(firstName);
            //            NSLog(@"First name= %@",first_name);
            NSMutableDictionary *dict_name = [[NSMutableDictionary alloc]init];
            if (!([first_name isEqualToString:@"(null)"]|| first_name == nil || first_name.length ==0))
            {
                [dict_name setValue:first_name forKey:@"FirstName"];

            }

            [first_name release];
            mid_name= ABRecordCopyValue(aSource, kABPersonMiddleNameProperty);
            //            NSLog(@"mid name = %@",mid_name);
            //            CFRelease(midname);
            if (!([mid_name isEqualToString:@"(null)"]|| mid_name == nil || mid_name.length ==0))
            {
                [dict_name setValue:mid_name forKey:@"MiddleName"];

            }
            [mid_name release];
            last_name = ABRecordCopyValue(aSource, kABPersonLastNameProperty);
            //            NSLog(@"Last Name= %@",last_name);
            //            CFRelease(lastName);
            if (!([last_name isEqualToString:@"(null)"]|| last_name == nil || last_name.length ==0))
            {
                [dict_name setValue:last_name forKey:@"LastName"];
            }
            [last_name release];
            NSString *dictNamestring = [NSString stringWithFormat:@"%@",dict_name];
            if (!([dictNamestring isEqualToString:@"(null)"]|| dictNamestring == nil || dictNamestring.length <4))
            {
                [dict setValue:dict_name forKey:@"Names"];
            }
            //            [dict_name release];

            // Getting Phone numbers from address book
            NSMutableDictionary *dict_phone = [[NSMutableDictionary alloc]init];
            ABMultiValueRef phones =(NSString*)ABRecordCopyValue(aSource, kABPersonPhoneProperty);
            NSString* mobileLabel;
            for(CFIndex i = 0; i < ABMultiValueGetCount(phones); i++) {
                mobileLabel = (NSString*)ABMultiValueCopyLabelAtIndex(phones, i);
                if([mobileLabel isEqualToString:(NSString *)kABPersonPhoneMobileLabel])
                {
                    home_mobile = (NSString*)ABMultiValueCopyValueAtIndex(phones, i) ;
                    [dict_phone setValue:home_mobile forKey:@"Mobile"];
                }

                else if ([mobileLabel isEqualToString:(NSString*)kABHomeLabel])
                {
                    basic_mobile = (NSString*)ABMultiValueCopyValueAtIndex(phones, i);
                    [dict_phone setValue:basic_mobile forKey:@"Home"];
                }

                else if([mobileLabel isEqualToString:(NSString *)kABWorkLabel])
                {
                    work_mobile = (NSString*)ABMultiValueCopyValueAtIndex(phones, i);
                    [dict_phone setValue:work_mobile forKey:@"Work"];
                }
                else if([mobileLabel isEqualToString:(NSString *)kABPersonPhoneIPhoneLabel])
                {
                    iphone = (NSString*)ABMultiValueCopyValueAtIndex(phones, i);
                    [dict_phone setValue:iphone forKey:@"iPhone"];
                }
                else if([mobileLabel isEqualToString:(NSString *)kABPersonPhoneMainLabel])
                {
                    main = (NSString*)ABMultiValueCopyValueAtIndex(phones, i);
                    [dict_phone setValue:main forKey:@"Main"];
                }

                else if ([mobileLabel isEqualToString:(NSString*)kABOtherLabel])
                {
                    other_phone = (NSString*)ABMultiValueCopyValueAtIndex(phones, i);
                    [dict_phone setValue:other_phone forKey:@"OtherPhone"];
                }
                else if ([mobileLabel isEqualToString:(NSString*)kABPersonPhonePagerLabel])
                {
                    pager_phone = (NSString*)ABMultiValueCopyValueAtIndex(phones, i);
                    [dict_phone setValue:pager_phone forKey:@"Pager"];
                }
                else
                {
                    NSLog(@"%@ label is not processed",mobileLabel);
                }
            }
            CFRelease(phones);
            NSString *dictphonestring = [NSString stringWithFormat:@"%@",dict_phone];
            if (!([dictphonestring isEqualToString:@"(null)"]|| dictphonestring == nil || dictphonestring.length <4))
            {
                [dict setValue:dict_phone forKey:@"PhoneNos"];
            }
            //            [dict_phone release];

            // Getting Fax numbers from address book
            NSMutableDictionary *dict_fax = [[NSMutableDictionary alloc]init];
            ABMultiValueRef faxes =(NSString*)ABRecordCopyValue(aSource, kABPersonPhoneProperty);
            NSString* FaxLabel;
            for(CFIndex i = 0; i < ABMultiValueGetCount(faxes); i++) {
                FaxLabel = (NSString*)ABMultiValueCopyLabelAtIndex(faxes, i);
                if([FaxLabel isEqualToString:(NSString *)kABPersonPhoneHomeFAXLabel])
                {
                    home_fax = (NSString*)ABMultiValueCopyValueAtIndex(faxes, i) ;
                    [dict_fax setValue:home_fax forKey:@"Home"];
                }
                if ([FaxLabel isEqualToString:(NSString*)kABPersonPhoneWorkFAXLabel])
                {
                    work_fax = (NSString*)ABMultiValueCopyValueAtIndex(faxes, i);
                    [dict_fax setValue:work_fax forKey:@"Work"];

                }
                if([FaxLabel isEqualToString:(NSString *)kABPersonPhoneOtherFAXLabel])
                {
                    other_fax = (NSString*)ABMultiValueCopyValueAtIndex(faxes, i);
                    [dict_fax setValue:other_fax forKey:@"Other"];
                }
            }
            CFRelease(faxes);
            NSString *dictfaxstring = [NSString stringWithFormat:@"%@",dict_fax];

            if (!([dictfaxstring isEqualToString:@"(null)"]|| dictfaxstring == nil || dictfaxstring.length <4))
            {
                [dict setValue:dict_fax forKey:@"FaxNos"];
            }
            [dict_fax release];

            // Getting emails from address book
            ABMultiValueRef email = ABRecordCopyValue(aSource, kABPersonEmailProperty);
            NSMutableDictionary *dict_email = [[NSMutableDictionary alloc]init];
            NSString* email_lbl;


            for(CFIndex i = 0; i < ABMultiValueGetCount(email); i++) {
                email_lbl = (NSString*)ABMultiValueCopyLabelAtIndex(email, i);

                if([email_lbl isEqualToString:(NSString *)kABHomeLabel])
      

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...