我正在使用此代码获取联系人列表,然后我想将联系人列表存储到一个数组中,好吧,如果我知道如何添加所有名字,那么我将继续使用其他属性.
- (void)viewDidLoad {
// Implement viewDidLoad if you need to do additional setup after loading the view.
[super viewDidLoad];
ABAddressBookRef addressBook = ABAddressBookCreate(); // create address book reference object
NSArray *abContactArray = (NSArray *)ABAddressBookCopyArrayOfAllPeople(addressBook); // get address book contact array
NSInteger totalContacts =[abContactArray count];
for(NSUInteger loop= 0 ; loop < totalContacts; loop++)
{
ABRecordRef record = (ABRecordRef)[abContactArray objectAtIndex:loop]; // get address book record
// NSLog(@"%@,%@,%@",recordIdString,firstNameString,lastNameString);
[myarray addObject:firstNameString];
NSLog(@"%@",[myarray objectAtIndex:1]);
if(ABRecordGetRecordType(record) == kABPersonType) // this check execute if it is person group
{
ABRecordID recordId = ABRecordGetRecordID(record); // get record id from address book record
recordIdString = [NSString stringWithFormat:@"%d",recordId]; // get record id string from record id
firstNameString = (NSString*)ABRecordCopyValue(record,kABPersonFirstNameProperty); // fetch contact first name from address book
lastNameString = (NSString*)ABRecordCopyValue(record,kABPersonLastNameProperty); // fetch contact last name from address book
//NSLog(@"%@,%@,%@",recordIdString,firstNameString,lastNameString);
}
}
}
myarray 是NSMutableArray *myarray
在 .h 类中创建的对象。
任何帮助将不胜感激。谢谢你。