我有一个问题。我想让我按下按钮,当我点击他时,他会将新联系人放入 iPhone AdressBook。我看到了很多教程,但我没有找到检查联系人是否已存在于 AdressBook 中的教程。
有人可以帮助我如何以编程方式进行此操作吗?我希望当有人单击按钮时,他首先检查或联系人已经存在于地址簿中。
我希望有人能帮助我。
非常感谢 ;-)
我有一个问题。我想让我按下按钮,当我点击他时,他会将新联系人放入 iPhone AdressBook。我看到了很多教程,但我没有找到检查联系人是否已存在于 AdressBook 中的教程。
有人可以帮助我如何以编程方式进行此操作吗?我希望当有人单击按钮时,他首先检查或联系人已经存在于地址簿中。
我希望有人能帮助我。
非常感谢 ;-)
使用ABAddressBookCopyPeopleWithName()
. 这将返回地址簿中的匹配列表,查看Iphone - 检查地址簿中是否已存在组名
ABAddressBookRef addressbook = ABAddressBookCreateWithOptions(Nil, Nil);
NSArray *people = (__bridge NSArray*)ABAddressBookCopyArrayOfAllPeople(addressbook);
NSMutableArray *namearray=[[NSMutableArray alloc] init];
for(id person in people){
NSString *firstNameString = (__bridge NSString*)ABRecordCopyValue((__bridge ABRecordRef)(person),kABPersonFirstNameProperty);
NSLog(@"%@",firstNameString);
[namearray addObject:firstNameString];
}
NSLog(@"%@",namearray);
NSString *yourString = name you want to add;
BOOL identicalStringFound = NO;
for (NSString *someString in namearray) {
if ([someString isEqual:yourString]) {
identicalStringFound = YES;
break;
}
}
if(identicalStringFound)
{
//code you want to perform
}
else
{
//code you want to perform
}
通过联系电话检查联系人是否退出
注意:仅用您的检查联系人号码替换checksPhoneNumber变量
ABAddressBookRef * addressbook = ABAddressBookCreateWithOptions(Nil, Nil);
NSArray *people = (__bridge NSArray*)ABAddressBookCopyArrayOfAllPeople(addressbook);
NSMutableArray *phoneArray=[[NSMutableArray alloc] init];
for(id person in people)
{
// get person contact number
ABMultiValueRef phones = (ABMultiValueRef)ABRecordCopyValue((__bridge ABRecordRef)(person), kABPersonPhoneProperty);
NSString* mobile=@"";
NSString* mobileLabel;
for (int i=0; i < ABMultiValueGetCount(phones); i++)
{
mobileLabel = (__bridge NSString*)ABMultiValueCopyLabelAtIndex(phones, i);
if([mobileLabel isEqualToString:(NSString *)kABPersonPhoneMobileLabel]) {
NSLog(@"mobile:");
} else if ([mobileLabel isEqualToString:(NSString*)kABPersonPhoneIPhoneLabel]) {
NSLog(@"iphone:");
} else if ([mobileLabel isEqualToString:(NSString*)kABPersonPhonePagerLabel]) {
NSLog(@"pager:");
}
mobile = (__bridge NSString*)ABMultiValueCopyValueAtIndex(phones, i);
NSLog(@"%@", mobile);
// remove all spaces bracket from contact number
NSMutableString *newPhoneStr = [[NSMutableString alloc] init];;
int j = [mobile length];
for (int i=0; i<j; i++)
{
if ([mobile characterAtIndex:i] >=48 && [mobile characterAtIndex:i] <=59)
{
[newPhoneStr appendFormat:@"%c",[mobile characterAtIndex:i]];
}
}
//add contact into phoneArray
[phoneArray addObject:newPhoneStr];
}
}
NSLog(@"%@",phoneArray);
BOOL identicalStringFound = NO;
// remove all spaces bracket from contact number which is check
NSMutableString *newCheckingPhoneNumberStr = [[NSMutableString alloc] init];
int j = [checkingPhoneNumber length];
for (int i=0; i<j; i++)
{
if ([checkingPhoneNumber characterAtIndex:i] >=48 && [[profileDetailsDict valueForKey:@"mobile"] characterAtIndex:i] <=59)
{
[newCheckingPhoneNumberStr appendFormat:@"%c",[checkingPhoneNumber characterAtIndex:i]];
}
}
for (NSString *contact in phoneArray)
{
if ([contact isEqual:newCheckingPhoneNumberStr])
{
identicalStringFound = YES;
break;
}
}
if(identicalStringFound)
{
// checkingPhoneNumber is exit
}
else
{
// checkingPhoneNumber is not exit
}