このコードを使用して連絡先リストを取得し、連絡先リストを配列に保存します。すべての名前を追加する方法がわかれば、最初の名前だけでいいので、先に進み、他のプロパティも追加します.
- (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 クラスで作成されたオブジェクトです。
どんな助けでも大歓迎です。ありがとうございました。