2

我正在使用下面的编码来获取字符串格式的名字,但想在一个数组中获取它,因为它包括地址簿联系人和 facebook 联系人。这样我就可以像数组 [0]、数组 [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 );
4

1 回答 1

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

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

如何将 ABRecordRef 添加到 iPhone 中的 NSMutableArray?

将地址簿联系人存储到字典中

于 2013-01-12T09:19:22.150 回答