Developer/iOS

[IPhone] 아이폰에서 주소록 가져오기.

블로blow 2011. 1. 12. 15:20
728x90

ABAddressBookRef addressBook = ABAddressBookCreate();

CFArrayRef allPeople = ABAddressBookCopyArrayOfAllPeople(addressBook);

CFIndex nPeople = ABAddressBookGetPersonCount(addressBook);


friend_info = [[NSMutableArray alloc]init]; 

//===================================================//

// 저장된 구조체를 돌면서 해당 데이터를 추출해 옵니다.

//===================================================//


for (int i = 0; i < nPeople ; i++) {

ABRecordRef ref = CFArrayGetValueAtIndex(allPeople, i);

CFStringRef firstName = ABRecordCopyValue(ref, kABPersonFirstNameProperty);

CFStringRef lastName = ABRecordCopyValue(ref, kABPersonLastNameProperty);


NSString *name = [NSString stringWithFormat:@"%@ %@", (lastName != nil) ? (NSString *)lastName : @"" ,(firstName != nil) ? (NSString *)firstName : @""];

if (name == nil) {

return;

}

// 이메일 리트를 구한다.

ABMultiValueRef emails = ABRecordCopyValue(ref, kABPersonEmailProperty);

// 이메일 리스트가 존재하지 않으면

if(ABMultiValueGetCount(emails) <= 0) { // the selected contact has no attached email address

[self dismissModalViewControllerAnimated:YES];

}

else{ //if(ABMultiValueGetCount(emails) == 1) { // the selected contact has exactly one email address

for(int i=0; i<ABMultiValueGetCount(emails); i++){

CFStringRef email = ABMultiValueCopyValueAtIndex(emails, i);

NSString *emailString = (NSString *) email;

//self.emailList.text = [self.emailList.text stringByAppendingString:[NSString stringWithFormat:@"%@ ", emailString]];

//NSLog(@"email = %@", emailString);

//이메일이 있는 사람들만 저장하도록 합니다.

if(emailString == nil){

}else {

NSDictionary *info = [[NSDictionary alloc] initWithObjectsAndKeys:

  name, @"NAME", emailString, @"EMAIL", kNotSelectTableCell, @"CHECK", nil];

email_person_cnt++;

[friend_info addObject:info];

[info release];

}

[emailString release];

[self dismissModalViewControllerAnimated:YES];

}

}

ABMultiValueRef phones = ABRecordCopyValue(ref, kABPersonPhoneProperty);

if(ABMultiValueGetCount(phones) <= 0) { // the selected contact has no attached email address

[self dismissModalViewControllerAnimated:YES];

}

else{ //if(ABMultiValueGetCount(emails) == 1) { // the selected contact has exactly one email address

for(int i=0; i<ABMultiValueGetCount(phones); i++){

CFStringRef phone = ABMultiValueCopyValueAtIndex(phones, i);

NSString *phoneString = (NSString *) phone;

if(phoneString == nil){

}else {

NSDictionary *info = [[NSDictionary alloc] initWithObjectsAndKeys:

  name, @"NAME", phoneString, @"EMAIL", kNotSelectTableCell, @"CHECK", nil];

email_person_cnt++;

[friend_info addObject:info];

[info release];

}

[phoneString release];

[self dismissModalViewControllerAnimated:YES];

}

}

}

CFRelease(allPeople);


더 자세히 알고싶으시면 댓글이나 방명록에 써주세요.

728x90