2

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.

JSA986
  • 5,870
  • 9
  • 45
  • 91
Prachi Rajput
  • 502
  • 1
  • 8
  • 20
  • 1
    I think you need to first fetch all contacts first and then need to find a number lies within that contacts or not. Check this http://stackoverflow.com/questions/19027118/fetch-contacts-in-ios-7 – DipakSonara Jun 11 '14 at 10:40
  • Heres a good [tutorial](http://www.appcoda.com/ios-programming-import-contact-address-book/) on fetching the Contacts list of the phone and then using that u can check whether to add or not.. – Ahmed Z. Jun 11 '14 at 10:42
  • Why down voted? I have found the way. – Prachi Rajput Jun 11 '14 at 12:38

5 Answers5

4

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;
    }

}
}
Prachi Rajput
  • 502
  • 1
  • 8
  • 20
  • 1
    it crashes at line (CFStringCompare(ABRecordCopyCompositeName(thisContact), with EXC_BAD_ACCESS error..can you please help – BhavikKama Mar 19 '15 at 11:32
0

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

JSA986
  • 5,870
  • 9
  • 45
  • 91
0
#import <AddressBook/AddressBook.h>
#import <AddressBookUI/AddressBookUI.h>

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

jxdwinter
  • 2,339
  • 6
  • 36
  • 56
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
0

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;
}
Ganpat
  • 768
  • 3
  • 15
  • 30