0

I have one question. I want make i button that when i click on him, he put the new contact into iPhone AdressBook. I see a lot of tutorials but i didn't found a tutorial with check if the contact already exist in the AdressBook.

Can somebody help me how to make this programmatically? I want that when somebody click on the button, he check first or the Contact already exist in the AdressBook.

I hope somebody can help me .

Thanks a lot ;-)

Muckel Buckel
  • 11
  • 1
  • 3

3 Answers3

0

Use ABAddressBookCopyPeopleWithName(). This will return list of matches from the Address Book, Take a look at Iphone - Check if a group name already exist in addressbook

Community
  • 1
  • 1
Parag Bafna
  • 22,812
  • 8
  • 71
  • 144
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
 }
Irshad
  • 445
  • 5
  • 16
0

Check Contact is Exit or Not By Contact Number

Note : Only replace checkingPhoneNumber variable by your checking contact number

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 
 }
Shrikant Tanwade
  • 1,391
  • 12
  • 21