Load all the Contacts from iphone PhoneBook in a variable

本秂侑毒 提交于 2019-12-02 21:39:10

问题


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 that. Any sample code would be highly appreciable.

Looking forward to a response.:)

Thanks in advance!!..... :)


回答1:


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


来源:https://stackoverflow.com/questions/4883248/load-all-the-contacts-from-iphone-phonebook-in-a-variable

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!