添加/检查联系xcode弧

时间:2013-06-03 10:29:49

标签: ios automatic-ref-counting addressbook

我有一个问题。我想让我按下按钮,当我点击他时,他将新联系人放入iPhone AdressBook。我看了很多教程,但是我没有找到一个教程,检查AdressBook中是否已存在联系人。

有人可以帮助我如何以编程方式进行此操作吗?我希望当有人点击按钮时,他会先检查,或者AdressBook中已存在联系人。

我希望有人可以帮助我。

非常感谢; - )

3 个答案:

答案 0 :(得分:0)

使用ABAddressBookCopyPeopleWithName()。这将返回地址簿中的匹配列表,请查看Iphone - Check if a group name already exist in addressbook

答案 1 :(得分:0)

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
 }

答案 2 :(得分:0)

检查联系人是否退出按联系号码

注意:只能用检查联系电话

替换 checkingPhoneNumber 变量
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 
 }