2013-04-01 138 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

您可以ABRecordCopyCompositeName得到該人的姓名,並測試如果搜索字符串包含在名稱以CFStringFind

例如,要查找聯繫人,在他的名字 「蘋果核戰記」:

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個月前被問到......

相關問題