iPhone 地址簿搜索使用号码
iPhone AddressBook Search Using number
我可以使用 phone number.But 搜索联系人姓名 issues.Like 如果地址簿中的号码有国家代码和号码,如果我只搜索编号 returns emty value.So 它无法搜索正确的 way.Here 是我完成的代码片段。
- (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;
}
我的问题是-假设我有一个联系人并且 phone 号码类似于 8801723432323
我正在使用此 01723432323
和以上代码 returns emty.Here 88 是 countrycode.Also 我需要在对面工作 too.Suppose 我给定的号码有国家代码,但地址簿中没有国家代码。
你可以使用[newPhoneNumber containsString:phnNumber]
代替[newPhoneNumber isEqualToString:phnNumber]
,但是你也应该改变你的逻辑,因为containsString方法可以匹配多个数字。
替换此行
if([newPhoneNumber isEqualToString:phnNumber])
和if ([newPhoneNumber containsString:phnNumber])
iOS8 及以后和
NSRange range = [newPhoneNumber rangeOfString:phnNumber]; if (range.length != 0)
适用于较早的设备。 containsString:
介绍于 iOS8。
我可以使用 phone number.But 搜索联系人姓名 issues.Like 如果地址簿中的号码有国家代码和号码,如果我只搜索编号 returns emty value.So 它无法搜索正确的 way.Here 是我完成的代码片段。
- (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;
}
我的问题是-假设我有一个联系人并且 phone 号码类似于 8801723432323
我正在使用此 01723432323
和以上代码 returns emty.Here 88 是 countrycode.Also 我需要在对面工作 too.Suppose 我给定的号码有国家代码,但地址簿中没有国家代码。
你可以使用[newPhoneNumber containsString:phnNumber]
代替[newPhoneNumber isEqualToString:phnNumber]
,但是你也应该改变你的逻辑,因为containsString方法可以匹配多个数字。
替换此行
if([newPhoneNumber isEqualToString:phnNumber])
和if ([newPhoneNumber containsString:phnNumber])
iOS8 及以后和
NSRange range = [newPhoneNumber rangeOfString:phnNumber]; if (range.length != 0)
适用于较早的设备。 containsString:
介绍于 iOS8。