2013-04-01 2 views
1

내 IOS 응용 프로그램에서 이름과 일치하는 연락처의 전화 번호를 찾고 싶습니다.연락처의 전화 번호를 찾는 방법은 무엇입니까? (Abaddressbook)

CFErrorRef *error = NULL; 

// Create a address book instance. 
ABAddressBookRef addressbook = ABAddressBookCreateWithOptions(NULL, error); 

// Get all people's info of the local contacts. 
CFArrayRef allPeople = ABAddressBookCopyArrayOfAllPeople(addressbook); 
CFIndex numPeople = ABAddressBookGetPersonCount(addressbook); 
for (int i=0; i < numPeople; i++) { 

    // Get the person of the ith contact. 
    ABRecordRef person = CFArrayGetValueAtIndex(allPeople, i); 
    ## how do i compare the name with each person object? 
} 

답변

0

검색 문자열이 CFStringFind와 이름에 포함되어있는 경우가 ABRecordCopyCompositeName으로 상대방의 이름을 얻고, 테스트 할 수 있습니다.

예를 들어

은 그의 이름에 "애플 시드"로 연락처를 찾을 수 있습니다 :이 도움이

CFStringRef contactToFind = CFSTR("Appleseed"); 

CFErrorRef *error = NULL; 

// Create a address book instance. 
ABAddressBookRef addressbook = ABAddressBookCreateWithOptions(NULL, error); 

// Get all people's info of the local contacts. 
CFArrayRef allPeople = ABAddressBookCopyArrayOfAllPeople(addressbook); 
CFIndex numPeople = ABAddressBookGetPersonCount(addressbook); 
for (int i=0; i < numPeople; i++) { 

    // Get the person of the ith contact. 
    ABRecordRef person = CFArrayGetValueAtIndex(allPeople, i); 

    // Get the composite name of the person 
    CFStringRef name = ABRecordCopyCompositeName(person); 

    // Test if the name contains the contactToFind string 
    CFRange range = CFStringFind(name, contactToFind, kCFCompareCaseInsensitive); 

    if (range.location != kCFNotFound) 
    { 
     // Bingo! You found the contact 
     CFStringRef message = CFStringCreateWithFormat(kCFAllocatorDefault, NULL, CFSTR("Found: %@"), name); 
     CFShow(message); 
     CFRelease(message); 
    } 

    CFRelease(name); 
} 

CFRelease(allPeople); 
CFRelease(addressbook); 

희망을. 귀하의 질문에 5 개월 전에 질문했지만 ...