如何保存一个名称字符串和两个不同长度的电子邮件和电话号码

时间:2015-08-03 07:49:36

标签: ios iphone addressbook

我想这是一个非常简单和基本的问题。

我正在使用#import <AddressBook/AddressBook.h>来获取设备的联系信息,我已成功实现了相同的信息。

当我收到一个电话号码和每个联系人发送一封电子邮件时,Everythings工作正常。但是当我得到多个电话号码和多个电子邮件时,我的应用程序崩溃了。我想我没有使用正确的保存方法。

所以我真的想知道如何保存一个名字字符串和一个电子邮件数组(不同长度)和电话号码(不同长度)作为联系人的组和所有其他人相同。因此,稍后在详细信息屏幕上重现结果并不困难

 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];

       }

现在我有三个数组,详细信息包含一个联系人的详细信息。现在,如何将这三个数组值合并为一个,以便保存和获取多个或数百个联系人的数据

1 个答案:

答案 0 :(得分:2)

ABAddressBookRef allPeople = ABAddressBookCreate();
CFArrayRef allContacts = ABAddressBookCopyArrayOfAllPeople(allPeople);
CFIndex numberOfContacts  = ABAddressBookGetPersonCount(allPeople);

NSLog(@"numberOfContacts------------------------------------%ld",numberOfContacts);


for(int i = 0; i < numberOfContacts; i++){
    NSString* name = @"";
    NSString* phone = @"";
    NSString* email = @"";

    ABRecordRef aPerson = CFArrayGetValueAtIndex(allContacts, i);
    ABMultiValueRef fnameProperty = ABRecordCopyValue(aPerson, kABPersonFirstNameProperty);
    ABMultiValueRef lnameProperty = ABRecordCopyValue(aPerson, kABPersonLastNameProperty);

    ABMultiValueRef phoneProperty = ABRecordCopyValue(aPerson, kABPersonPhoneProperty);\
    ABMultiValueRef emailProperty = ABRecordCopyValue(aPerson, kABPersonEmailProperty);

    NSArray *emailArray = (__bridge NSArray *)ABMultiValueCopyArrayOfAllValues(emailProperty);
    NSArray *phoneArray = (__bridge NSArray *)ABMultiValueCopyArrayOfAllValues(phoneProperty);


    if (fnameProperty != nil) {
        name = [NSString stringWithFormat:@"%@", fnameProperty];
    }
    if (lnameProperty != nil) {
        name = [name stringByAppendingString:[NSString stringWithFormat:@" %@", lnameProperty]];
    }

    if ([phoneArray count] > 0) {
        if ([phoneArray count] > 1) {
            for (int i = 0; i < [phoneArray count]; i++) {
                phone = [phone stringByAppendingString:[NSString stringWithFormat:@"%@\n", [phoneArray objectAtIndex:i]]];
            }
        }else {
            phone = [NSString stringWithFormat:@"%@", [phoneArray objectAtIndex:0]];
        }
    }

    if ([emailArray count] > 0) {
        if ([emailArray count] > 1) {
            for (int i = 0; i < [emailArray count]; i++) {
                email = [email stringByAppendingString:[NSString stringWithFormat:@"%@\n", [emailArray objectAtIndex:i]]];
            }
        }else {
            email = [NSString stringWithFormat:@"%@", [emailArray objectAtIndex:0]];
        }
    }

    NSLog(@"NAME : %@",name);
    NSLog(@"PHONE: %@",phone);
    NSLog(@"EMAIL: %@",email);
    NSLog(@"\n");
}

如果有联系人,您可以在日志中看到多个联系人和电子邮件。