iOS7부터는 전화번호부 접근시 사용자 동의를 필요로 한다. 책에 있던 코드만으로는 부족해서 구글링하며 고친 코드.

거의 다 참고해서 만든 것이라 이렇게 올려도 되나 싶지만, 혹시라도 도움이 될까 올림.



char* ConvertCFStringCopyUTF8String(CFStringRef cfString){

    if(cfString == NULL){

        return NULL;

    }

    

    CFIndex length = CFStringGetLength(cfString);

    CFIndex maxSize = CFStringGetMaximumSizeForEncoding(length, kCFStringEncodingUTF8);

    char* pBuffer = (char*)malloc(maxSize);

    

    if(CFStringGetCString(cfString, pBuffer, maxSize, kCFStringEncodingUTF8)){

        return pBuffer;

    }

    

    return NULL;

}


- (void)getFriendData:(void*)friendDatas{

    std::vector<FriendData*>* userFriendDatas = (std::vector<FriendData*>*)friendDatas;

    

    // Request to authorise the app to use addressbook

    ABAddressBookRef addressBookRef = ABAddressBookCreateWithOptions(nil, nil);

    

    if (ABAddressBookGetAuthorizationStatus() == kABAuthorizationStatusNotDetermined) {

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

            if (granted) {

                // If the app is authorized to access the first time then add the contact

                //[self _addContactToAddressBook];

                

                //ABAddressBookRef addressBook = ABAddressBookCreate();

                //CFErrorRef *error = NULL;

                //ABAddressBookRef addressBook = ABAddressBookCreateWithOptions(NULL, error);

                CFArrayRef allPeople = ABAddressBookCopyArrayOfAllPeople(addressBookRef);

                CFIndex nPeople = ABAddressBookGetPersonCount(addressBookRef);

                

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

                    ABRecordRef ref = CFArrayGetValueAtIndex(allPeople, i);

                    CFStringRef firstName = (CFStringRef)ABRecordCopyValue(ref, kABPersonFirstNameProperty);

                    CFStringRef lastName = (CFStringRef)ABRecordCopyValue(ref, kABPersonLastNameProperty);

                    

                    FriendData* pFriendData = new FriendData();

                    

                    if(firstName){

                        char* pFirstName = ConvertCFStringCopyUTF8String(firstName);

                        if(pFirstName != NULL){

                            pFriendData->name = pFirstName;

                            pFriendData->name += " ";

                        }

                        free(pFirstName);

                        

                        if(firstName != nil){

                            CFRelease(firstName);

                        }

                    }

                    

                    if(lastName){

                        char* pLastName = ConvertCFStringCopyUTF8String(lastName);

                        if(pLastName != NULL){

                            pFriendData->name += pLastName;

                        }

                        free(pLastName);

                        

                        if(lastName != nil){

                            CFRelease(lastName);

                        }

                        

                        if(pFriendData->name.length() == 0){

                            delete pFriendData;

                            continue;

                        }

                    }

                    

                    ABMultiValueRef phoneNums = (ABMultiValueRef)ABRecordCopyValue(ref, kABPersonPhoneProperty);

                    

                    bool bFoundPhoneNumber = false;

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

                        CFStringRef label = ABMultiValueCopyLabelAtIndex(phoneNums, j);

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

                        

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

                            if(tempRef != nil){

                                bFoundPhoneNumber = true;

                                

                                char* pPhoneNumber = ConvertCFStringCopyUTF8String(tempRef);

                                pFriendData->phoneNumber = pPhoneNumber;

                                free(pPhoneNumber);

                            }

                        }

                        

                        CFRelease(label);

                        CFRelease(tempRef);

                    }

                    

                    if(bFoundPhoneNumber == false || pFriendData->phoneNumber.length() == 0){

                        delete pFriendData;

                    }

                    else{

                        userFriendDatas->push_back(pFriendData);

                    }

                }

                

                CFRelease(allPeople);


            } else {

                // Show an alert here if user denies access telling that the contact cannot be added because you didn't allow it to access the contacts

            }

        });

    }

    else if (ABAddressBookGetAuthorizationStatus() == kABAuthorizationStatusAuthorized) {

        // If the user user has earlier provided the access, then add the contact

        //[self _addContactToAddressBook];

        

        //ABAddressBookRef addressBook = ABAddressBookCreate();

        CFErrorRef *error = NULL;

        ABAddressBookRef addressBook = ABAddressBookCreateWithOptions(NULL, error);

        CFArrayRef allPeople = ABAddressBookCopyArrayOfAllPeople(addressBook);

        CFIndex nPeople = ABAddressBookGetPersonCount(addressBook);

        

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

            ABRecordRef ref = CFArrayGetValueAtIndex(allPeople, i);

            CFStringRef firstName = (CFStringRef)ABRecordCopyValue(ref, kABPersonFirstNameProperty);

            CFStringRef lastName = (CFStringRef)ABRecordCopyValue(ref, kABPersonLastNameProperty);

            

            FriendData* pFriendData = new FriendData();

            

            if(firstName){

                char* pFirstName = ConvertCFStringCopyUTF8String(firstName);

                if(pFirstName != NULL){

                    pFriendData->name = pFirstName;

                    pFriendData->name += " ";

                }

                free(pFirstName);

                

                if(firstName != nil){

                    CFRelease(firstName);

                }

            }

            

            if(lastName){

                char* pLastName = ConvertCFStringCopyUTF8String(lastName);

                if(pLastName != NULL){

                    pFriendData->name += pLastName;

                }

                free(pLastName);

                

                if(lastName != nil){

                    CFRelease(lastName);

                }

                

                if(pFriendData->name.length() == 0){

                    delete pFriendData;

                    continue;

                }

            }

            

            ABMultiValueRef phoneNums = (ABMultiValueRef)ABRecordCopyValue(ref, kABPersonPhoneProperty);

            

            bool bFoundPhoneNumber = false;

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

                CFStringRef label = ABMultiValueCopyLabelAtIndex(phoneNums, j);

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

                

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

                    if(tempRef != nil){

                        bFoundPhoneNumber = true;

                        

                        char* pPhoneNumber = ConvertCFStringCopyUTF8String(tempRef);

                        pFriendData->phoneNumber = pPhoneNumber;

                        free(pPhoneNumber);

                    }

                }

                

                CFRelease(label);

                CFRelease(tempRef);

            }

            

            if(bFoundPhoneNumber == false || pFriendData->phoneNumber.length() == 0){

                delete pFriendData;

            }

            else{

                userFriendDatas->push_back(pFriendData);

            }

        }

        

        CFRelease(allPeople);


    }

    else {

        // If the user user has NOT earlier provided the access, create an alert to tell the user to go to Settings app and allow access

        

        UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"연락처 접근 실패" message:@"Access to the addressbook is currently restricted. Setting>Privacy>contacts 에서 수정해주세요." delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil];

        [alert show];

        [alert release];

    }

    

}

+ Recent posts