Prachi Rajput
Prachi Rajput

Reputation: 512

Is it possible to check a contact whether it is exits in contact list of iPhone or not?

I am developing an app in which i have to save contact in address book, but if the the contact is already saved in contact then it should not save.

But i have no idea is it possible to check a contact whether it is exits in contact list of iPhone or not?

Any help would be appreciated.

Thanks in advance.

Upvotes: 2

Views: 1246

Answers (5)

Ganpat
Ganpat

Reputation: 863

If anybody wants to check name is saved in address book or not, then below function may be useful:

-(BOOL)isNameSaved:(NSString*)strGivenName {
    BOOL isSaved = NO;
    ABAddressBookRef addressBook = ABAddressBookCreate();
    CFArrayRef allPeople = ABAddressBookCopyArrayOfAllPeople(addressBook);
    CFIndex nPeople = ABAddressBookGetPersonCount(addressBook);
    for ( int i = 0; i < nPeople; i++ ) {
        ABRecordRef ref = CFArrayGetValueAtIndex(allPeople, i);
        CFStringRef firstc = (CFStringRef)ABRecordCopyValue(ref, kABPersonFirstNameProperty);
        NSString *first = [NSString stringWithFormat:@"%@",firstc];
        if ([first isEqualToString:strGivenName]) {
            isSaved = YES;
            break;
        }
    }
    return isSaved;
}

Upvotes: 0

Shrikant Tanwade
Shrikant Tanwade

Reputation: 1441

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 
 }

Upvotes: 0

Prachi Rajput
Prachi Rajput

Reputation: 512

Solved issue

 -(void)CheckContactIsExits{
  ABAddressBookRef addressBook = ABAddressBookCreateWithOptions(NULL, NULL);

NSArray *allContacts = (__bridge NSArray *)ABAddressBookCopyArrayOfAllPeople(addressBook);

ABRecordRef pet = ABPersonCreate();
ABRecordSetValue(pet, kABPersonFirstNameProperty, (__bridge CFStringRef)@"VoxSci Activation", nil);

for (id record in allContacts){
    ABRecordRef thisContact = (__bridge ABRecordRef)record;
    if (CFStringCompare(ABRecordCopyCompositeName(thisContact),
                        ABRecordCopyCompositeName(pet), 0) == kCFCompareEqualTo){

        NSLog(@"The contact already exists");
        //The contact already exists!
        isContactExits=YES;
    }

}
}

Upvotes: 4

jxdwinter
jxdwinter

Reputation: 2369

#import <AddressBook/AddressBook.h>
#import <AddressBookUI/AddressBookUI.h>

And read all the contacts in iPhone and check the name or phone exit or not.

Upvotes: 0

JSA986
JSA986

Reputation: 5936

Assuming your using Apples framework for this your user will be given the option to "create new contact" or if its already in the contacts list "add to existing contact". Therefore the user can decide if it should be added or not

Upvotes: 0

Related Questions