【发布时间】:2015-12-21 10:29:09
【问题描述】:
我可以使用电话号码搜索联系人姓名。但是存在一些问题。例如,如果地址簿中的号码有国家代码和号码,如果我只搜索号码,它会返回 emty 值。所以它不能搜索正确的方法。这是我所做的代码片段。
- (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。这里 88 是国家代码。我也需要对面工作。假设我给定的号码有国家代码,而地址簿中没有国家代码。
【问题讨论】:
标签: ios objective-c iphone addressbook