我能夠使用電話號碼搜索聯繫人姓名,但存在一些問題。如果地址簿中的號碼具有國家代碼和號碼,並且如果我僅使用號碼搜索,則返回emty值所以它不能以正確的方式搜索。這裏是我所做的snipet代碼。iPhone地址簿搜索使用號碼
- (ABRecordRef)findRecord:(NSString *)phnNumber
{
if (phnNumber == nil)
return nil;
CFErrorRef error = nil;
ABAddressBookRef addressBook = ABAddressBookCreateWithOptions(NULL, &error);
if (!addressBook) {
NSLog(@"null");
} else if (error) {
NSLog(@"error %@",error);
}
// Requests access to address book data from the user
ABAddressBookRequestAccessWithCompletion(addressBook, ^(bool granted, CFErrorRef error) {});
CFArrayRef all = ABAddressBookCopyArrayOfAllPeople(addressBook);
CFIndex n = ABAddressBookGetPersonCount(addressBook);
ABRecordRef record;
int count = 0;
for(int i = 0 ; i < n ; i++)
{
ABRecordRef ref = CFArrayGetValueAtIndex(all, i);
ABMultiValueRef phones = ABRecordCopyValue(ref, kABPersonPhoneProperty);
for(CFIndex j = 0; j < ABMultiValueGetCount(phones); j++)
{
CFStringRef phoneNumberRef = ABMultiValueCopyValueAtIndex(phones, j);
NSString *newPhoneNumber = (__bridge NSString *)phoneNumberRef;
NSLog(@"%@",newPhoneNumber);
if([newPhoneNumber isEqualToString:phnNumber])
{
record = ref;
i=(int)n;
count = 1;
}
CFRelease(phoneNumberRef);
}
}
if(count != 1)
{
ABRecordRef newPerson = ABPersonCreate();
ABMutableMultiValueRef multiPhone = ABMultiValueCreateMutable(kABMultiStringPropertyType);
ABMultiValueAddValueAndLabel(multiPhone, (__bridge CFTypeRef)(phnNumber), kABHomeLabel, NULL);
ABRecordSetValue(newPerson, kABPersonPhoneProperty, multiPhone,nil);
CFRelease(multiPhone);
record = newPerson;
}
return record;
}
和我打電話這樣說,這對名字:
ABRecordRef record = [self findRecord:@"Number_here"];
if(record){
NSString *name =(__bridge NSString *)ABRecordCopyCompositeName(record);
lbCalleName.text=name;
}
我的問題是覺得─假設我有一個聯繫人和電話號碼就像是8801723432323
,我這個01723432323
及以上搜索代碼返回emty.Here 88是countrycode.Also我也需要相反的工作。假設我的給定號碼在通訊錄中有國家代碼和國家代碼。
我不明白我的觀點。我告訴過如何看國家代碼。假如我的給定號碼在地址簿中有國家代碼和國家代碼,那麼會發生什麼?我需要這兩個工作。 –
試試看看這兩個庫: https://github.com/iziz/libPhoneNumber-iOS https://github.com/googlei18n/libphonenumber。 – lorenzoliveto