ABAddressBook store values in NSDictionary

前端 未结 3 1600
执念已碎
执念已碎 2021-01-07 04:30

I have an app that displays ABAddressBook contacts in a UITableView. Currently I\'m reading the contacts into an NSDictionary, however

3条回答
  •  伪装坚强ぢ
    2021-01-07 05:06

    You can use following way,

    ABAddressBookRef ab = ABAddressBookCreateWithOptions(NULL, NULL);
    NSArray *arrTemp = (NSArray *)ABAddressBookCopyArrayOfAllPeople(ab);
    

    The above 2 lines will create an array for all your contacts on the iPhone.

    Now whatever property of a contact you want to display you can display by using the below code. For example, I want to display the first name of all contacts and then create one Mutable array called it arrContact.

    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;
        }
        [arrContact addObject:dicContact];
        [dicContact release];
    }
    

    Now just display it using the arrContact array in a table view..

提交回复
热议问题