아이폰2013. 1. 18. 23:37

글을 올린 블로거를 생각해서 광고 한번만 클릭해주시면 감사하겠습니다

addressbook 프레임워크 추가해주세요

헤더파일에 전역변수로

NSMutableArray *nameArr;

NSMutableArray *phoneArr;

선언

#import <AddressBook/AddressBook.h>

- (void)viewDidLoad

{

[super viewDidLoad];

[self StartContacts];

}

- (void) StartContacts {

ABAddressBookRef ref;

if([[[UIDevice currentDevice] systemVersion] floatValue] >= 6.0) {

CFErrorRef error = nil;

ref = ABAddressBookCreateWithOptions(NULL,&error);

if (ABAddressBookGetAuthorizationStatus() == kABAuthorizationStatusNotDetermined) {

ABAddressBookRequestAccessWithCompletion(ref, ^(bool granted, CFErrorRef error) {

dispatch_async(dispatch_get_main_queue(), ^{

if (granted) {

[self RefContacts:ref];

CFRelease(ref);

} else {

NSLog(@"비활성, 오류 마음에 드시는 메시지를 써넣으세요");

}

});

});

} else if (ABAddressBookGetAuthorizationStatus() == kABAuthorizationStatusAuthorized) {

[self RefContacts:ref];

CFRelease(ref);

} else {

NSLog(@"장치의 설정 - 개인정보 보호 - 연락처 정보를 활성화 해주세요");

}

} else {

ref = ABAddressBookCreate();

[self RefContacts:ref];

CFRelease(ref);

}

}

- (void) RefContacts:(ABAddressBookRef) addressBook {

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

// 주소록의 모든 정보를 구조체에 저장을 합니다.

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

// ABAddressBookRef addressBook = ABAddressBookCreate();

CFArrayRef allPeople = ABAddressBookCopyArrayOfAllPeople(addressBook);

CFIndex nPeople = ABAddressBookGetPersonCount(addressBook);

//이름순으로 정렬

CFMutableArrayRef peopleMutable = CFArrayCreateMutableCopy(kCFAllocatorDefault,CFArrayGetCount(allPeople),allPeople);

CFArraySortValues(peopleMutable,CFRangeMake(0, CFArrayGetCount(peopleMutable)),(CFComparatorFunction) ABPersonComparePeopleByName,(void*) ABPersonGetSortOrdering());

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

// 저장된 구조체에서 해당 자료를 추출해 옵니다.

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

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

ABRecordRef ref = CFArrayGetValueAtIndex(peopleMutable, i);

CFStringRef firstName = ABRecordCopyValue(ref, kABPersonFirstNameProperty);

CFStringRef lastName = ABRecordCopyValue(ref, kABPersonLastNameProperty);

NSLog(@"Name : %@ %@", (firstName != nil) ? (NSString *)firstName : @"",

(lastName != nil) ? (NSString *)lastName : @"");

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

NSLog(@"NAME : %@",name);

[nameArr addObject:name];

if (firstName != nil)

CFRelease(firstName);

if (lastName != nil)

CFRelease(lastName);

// 사진이미지는 여기에 넘어옵니다.

if (ABPersonHasImageData(ref)) {

NSData *contactImageData = (NSData*)ABPersonCopyImageDataWithFormat(ref,

kABPersonImageFormatThumbnail);

UIImage *image = [[UIImage alloc] initWithData:contactImageData];

[contactImageData release];

// image 저장하는 펑션은 여기에 작성하시면 됩니다.

[image release];

}

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

// 전화번호 구조체 카테고리 저장/추출

// 전화번호 구조체에는 전화번호와 집전화, 핸드폰 이런 카테고리 구분이 있으며

// 이것은 Label 구별합니다.

// Label : kABHomeLabel, kABPersonPhoneIPhoneLabel 등등...

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

ABMultiValueRef phoneNums =

(ABMultiValueRef)ABRecordCopyValue(ref, kABPersonPhoneProperty);

for (CFIndex j = 0; j < ABMultiValueGetCount(phoneNums); j++) {

CFStringRef label = ABMultiValueCopyLabelAtIndex(phoneNums, j);

CFStringRef tempRef = (CFStringRef)ABMultiValueCopyValueAtIndex(phoneNums, j);

// 전화번호의 형태라벨별로 추출. 다른 형식도 이렇게 추출이 됩니다.

if (CFStringCompare(label, kABPersonPhoneMobileLabel, 0) ==

kCFCompareEqualTo) {

if (tempRef != nil)

NSLog(@"Mobile: %@", (NSString *)tempRef);

[phoneArr addObject:(NSString *)tempRef];

} else if (CFStringCompare(label, kABPersonPhoneIPhoneLabel, 0) ==

kCFCompareEqualTo) {

if (tempRef != nil)

NSLog(@"iPhone: %@", (NSString *)tempRef);

} else if (CFStringCompare(label, kABHomeLabel, 0) ==

kCFCompareEqualTo) {

if (tempRef != nil)

NSLog(@"Home: %@", (NSString *)tempRef);

}

CFRelease(label);

CFRelease(tempRef);

}

}

CFRelease(allPeople);

CFRelease(peopleMutable);

NSLog(@"nameArr : %@",nameArr);

NSLog(@"phoneArr : %@",phoneArr);

}

출처 : http://messace.tistory.com/56

Posted by 퍼플카우D