Developer/iOS

[iOS]주소록 가져오기 Addressbook controll by ABAddressBookRef.

블로blow 2013. 7. 8. 18:53
728x90



ABAddressBookRef addressBook = ABAddressBookCreate();

    CFArrayRef people  = ABAddressBookCopyArrayOfAllPeople(addressBook);

    for(int i = 0;i<ABAddressBookGetPersonCount(addressBook);i++)

    {

        ABRecordRef ref = CFArrayGetValueAtIndex(people, i);

        // Get First name, Last name, Prefix, Suffix, Job title 

        NSString *firstName = (NSString *)ABRecordCopyValue(ref,kABPersonFirstNameProperty);

        NSString *lastName = (NSString *)ABRecordCopyValue(ref,kABPersonLastNameProperty);

        

        if(firstName == nil)

            firstName = @"";

        if(lastName == nil)

            lastName = @"";

        

        NSString *fullName = [NSString stringWithFormat:@"%@%@", lastName, firstName];

        

        NSMutableDictionary *tmpDic = [[NSMutableDictionary allocinit];

        if(fullName != nil)

            [tmpDic setObject:fullName forKey:@"name"];

        

        ABMultiValueRef phones = ABRecordCopyValue(ref, kABPersonPhoneProperty);

        for(CFIndex j = 0; j < ABMultiValueGetCount(phones); j++)

        {       

            CFStringRef phoneNumberRef = ABMultiValueCopyValueAtIndex(phones, j);   

            NSString *phoneLabel =(NSString*) ABAddressBookCopyLocalizedLabel(ABMultiValueCopyLabelAtIndex(phones, j));

            if([phoneLabel isEqualToString:@"mobile"]) {

                NSString *phoneNumber = (NSString *)phoneNumberRef; 

                if(phoneNumber != nil)

                    [tmpDic setObject:phoneNumber forKey:@"mobile"];

            }

        }

        [self.arr_contact addObject:tmpDic];

        [self.arr_tmpContact addObject:tmpDic];


728x90