Load all the Contacts from iphone PhoneBook in a variable

前端 未结 1 1677
借酒劲吻你
借酒劲吻你 2021-01-26 13:13

I am naive to the xcode programming. I need to load all the contacts from the iPhone PhoneBook on a variable.

Could you please suggest me some library or something like

相关标签:
1条回答
  • 2021-01-26 13:21

    Hey all after a long fight I found the following code working

        self.dataSource = [[NSMutableArray alloc]init]; // dataSouce is delared in .h file
    
    ABAddressBookRef addressBook = ABAddressBookCreate(); 
    NSMutableArray *allPeople = (NSMutableArray *)ABAddressBookCopyArrayOfAllPeople(addressBook); 
    int nPeople = ABAddressBookGetPersonCount(addressBook); 
    
    for(int i=0; i < nPeople; i++ ){
        ABRecordRef person = [allPeople objectAtIndex:i];
        NSString *name = @"";
        if(ABRecordCopyValue(person, kABPersonFirstNameProperty) != NULL)
            name = [[NSString stringWithFormat:@"%@", ABRecordCopyValue(person, kABPersonFirstNameProperty)] stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceCharacterSet]];
    
        [dataSource addObject: name];
    }
    
    0 讨论(0)
提交回复
热议问题