フレームワーク:
#import "AddressBook/AddressBook.h"
コード:
- (void) getLocalContacts
{
ABAddressBookRef addressBook = ABAddressBookCreate( );
CFArrayRef allPeople = ABAddressBookCopyArrayOfAllPeople( addressBook );
CFIndex nPeople = ABAddressBookGetPersonCount( addressBook );
User *user;
NSMutableArray *allContacts = [[NSMutableArray alloc] init];
for ( int i = 0; i < nPeople; i++ )
{
ABRecordRef person = CFArrayGetValueAtIndex( allPeople, i );
ABMultiValueRef emails = ABRecordCopyValue(person, kABPersonEmailProperty);
if(ABMultiValueGetCount(emails) != 0)
{
user = [[User alloc] init];
CFStringRef fName, lName;
fName = ABRecordCopyValue(person, kABPersonFirstNameProperty);
lName = ABRecordCopyValue(person, kABPersonLastNameProperty);
CFStringRef email = ABMultiValueCopyValueAtIndex(emails, 0);
NSData *imgData = (NSData *)ABPersonCopyImageData(person);
NSString *firstName = (NSString *) fName;
NSString *lastName = (NSString *) lName;
if (firstName.length == 0 && lastName.length != 0){
user.userName = lastName;
}
else if (firstName.length != 0 && lastName.length == 0){
user.userName = firstName;
}
else if (firstName.length == 0 && lastName.length == 0){
user.userName = @"";
}
else if (firstName.length != 0 && lastName.length != 0){
user.userName = [NSString stringWithFormat:@"%@ %@", firstName, lastName];
}
//user.firstName = (NSString *) firstName;
user.lastName = @"";
user.email = (NSString *) email;
user.firstName = (NSString *) email;
user.localImage = [UIImage imageWithData:imgData];
[allContacts addObject:user];
[user release];
}
}
[DataManager sharedManager].allLocalUsers = allContacts;
[self hideSpinner];
}