在iPhone中的数组中存储地址簿联系人

时间:2013-01-12 09:05:55

标签: iphone ios addressbook

我正在使用以下编码来获取字符串格式的名字,但是想要在数组中获取它,因为它包括地址簿联系人和Facebook联系人。所以我可以像array [0],array [1]那样快速检索它们。所以请帮助我完成这项工作。

    ABAddressBookRef ab=ABAddressBookCreate();

    NSArray *arrTemp=(NSArray *)ABAddressBookCopyArrayOfAllPeople(ab);

    NSMutableArray *arrContact=[[NSMutableArray alloc] init];
       for (int i=0;i<[arrTemp count];i++) 
             {
    NSMutableDictionary *dicContact=[[NSMutableDictionary alloc] init];
    NSString *str=(NSString *) ABRecordCopyValue([arrTemp objectAtIndex:i],             kABPersonFirstNameProperty);
    @try
        {
    [dicContact setObject:str forKey:@"name"];
        }
    @catch (NSException * e) {
    [dicContact release];
    continue;
     }

    [dicContact release];

    NSLog(@"%@",str );

1 个答案:

答案 0 :(得分:1)

您可以存储您的地址将所有联系人保存到NSMutableArray中,如下所示: -

ABAddressBookRef addressBook = ABAddressBookCreate();
NSArray *thePeople = (NSArray *)ABAddressBookCopyArrayOfAllPeople(addressBook);
NSMutableArray* allPeoplesDicts = [NSMutableArray array];
for (id person in thePeople)
{
    ABMultiValueRef phones =(NSString*)ABRecordCopyValue(person, kABPersonPhoneProperty);
    NSString* name = (NSString *)ABRecordCopyCompositeName(person);
    NSMutableArray* phones = [[NSMutableArray alloc] init];
    for (CFIndex i = 0; i < ABMultiValueGetCount(phones); i++)
    {
        NSString *phone = [(NSString *)ABMultiValueCopyValueAtIndex(phones,i) autorelease];
        [phones addObject:phone];
    }
    NSDictionary* personDict = [[NSDictionary alloc] initWithObjectsAndKeys:name,@"Name",phones,@"PhoneNumbers",nil];
    [phones release];
    [allPeoplesDicts addObject:personDict];
    [personDict release];
}

我只是谷歌它,我访问了这个类似的问题: -

how can I Add a ABRecordRef to a NSMutableArray in iPhone?

storing adressbook contacts into a nsdictionary