lundi 3 août 2015

how to save one name string and two different length array of email and phone no

As i guess this is a very simple and basic question.

i am using #import <AddressBook/AddressBook.h> to get the contact info of device and i had successfully implemented the same.

Everythings work fine when i got one phone no and one email from each contact. But when i got multiple phone no's and multiple email then getting crash on my app. I guess i am not using the proper saving method.

So i really want to know how to save one name string and one array of email(different length) and phone no(different length) as group for a contact and same for all other. so that it would not be difficult to reproduce the result later on the detail screen

 CFErrorRef *error = NULL;
ABAddressBookRef addressBook = ABAddressBookCreateWithOptions(NULL, error);
CFArrayRef allPeople = ABAddressBookCopyArrayOfAllPeople(addressBook);
CFIndex numberOfPeople = ABAddressBookGetPersonCount(addressBook);

for(int i = 0; i < numberOfPeople; i++) {

    ABRecordRef person = CFArrayGetValueAtIndex( allPeople, i );

    NSString *firstName = (__bridge NSString *)(ABRecordCopyValue(person, kABPersonFirstNameProperty));
    NSString *lastName = (__bridge NSString *)(ABRecordCopyValue(person, kABPersonLastNameProperty));
    [values1 addObject:[NSString stringWithFormat:@"%@ %@",firstName,lastName]]; //values1 array to save name of contact

    ABMultiValueRef emails = ABRecordCopyValue(person, kABPersonEmailProperty);
    for (CFIndex j=0; j < ABMultiValueGetCount(emails); j++) {
        NSString* email = (__bridge NSString*)ABMultiValueCopyValueAtIndex(emails, j);
        NSLog(@"Email:%@", email);
        [values2 addObject:[NSString stringWithFormat:@"%@",email]];
    } //values2 array to save email associated to that contact

    ABMultiValueRef phoneNumbers = ABRecordCopyValue(person, kABPersonPhoneProperty);

    for (CFIndex i = 0; i < ABMultiValueGetCount(phoneNumbers); i++) {
        NSString *phoneNumber = (__bridge_transfer NSString *) ABMultiValueCopyValueAtIndex(phoneNumbers, i);
        NSLog(@"phone:%@", phoneNumber);
        [values3 addObject:[NSString stringWithFormat:@"%@",phoneNumber]];
    }//values3 array to save different phone no's associated to that contacts


    NSDictionary *dict = [[NSDictionary alloc]init];

       }

AS now i have three array with details contain details of one contact. Now how can merge these three array values to one so that it would be easy to save and fetch data for multiples or hundreds of contacts

Aucun commentaire:

Enregistrer un commentaire