-2

I am developing an application in which I want to fetch contacts who are using the same application from my contact list in iphone.

How to do it? Any sample code or link ?

Please help me.

Note : I don't want to fetch all contacts in my ios application, I just want to fetch the contacts who are using the same application.

Aarti Oza
  • 1,134
  • 2
  • 14
  • 31
  • 1
    Possible duplicate of [Get a list of all contacts on iOS](http://stackoverflow.com/questions/3747844/get-a-list-of-all-contacts-on-ios) – Nimit Parekh Nov 16 '15 at 11:28
  • @NimitParekh I don't want to retrive all contacts, I just want to retrive the contacts who are using the same application. – Aarti Oza Nov 16 '15 at 11:33
  • When you adding contact programmatically that time need to add some text and when fetching the contact that time you can filter out those record. – Nimit Parekh Nov 16 '15 at 12:12
  • you would need an online database to store the users of your app and their email or other unique token (so they would need to register as users). then fetch all contacts and filter for the known tokens. this is impossible to do just 'within' an app. – katzenhut Nov 16 '15 at 12:30

2 Answers2

0

import AddressBook framework first

Then call these two functions

-(void)AddressBookFetch{
    ABAuthorizationStatus status = ABAddressBookGetAuthorizationStatus();
    if (status == kABAuthorizationStatusDenied || status == kABAuthorizationStatusRestricted) {
        // if you got here, user had previously denied/revoked permission for your
        // app to access the contacts, and all you can do is handle this gracefully,
        // perhaps telling the user that they have to go to settings to grant access
        // to contacts
        [[[UIAlertView alloc] initWithTitle:nil message:@"This app requires access to your contacts to function properly. Please visit to the \"Privacy\" section in the iPhone Settings app." delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil] show];
        return;
    }

    CFErrorRef error = NULL;
    ABAddressBookRef addressBook = ABAddressBookCreateWithOptions(NULL, &error);
    if (!addressBook) {
        NSLog(@"ABAddressBookCreateWithOptions error: %@", CFBridgingRelease(error));
        return;
    }

    ABAddressBookRequestAccessWithCompletion(addressBook, ^(bool granted, CFErrorRef error) {
            if (error) {
                NSLog(@"ABAddressBookRequestAccessWithCompletion error: %@", CFBridgingRelease(error));
            }

            if (granted) {
                // if they gave you permission, then just carry on

                [self listPeopleInAddressBook:addressBook];
            } else {
                // however, if they didn't give you permission, handle it gracefully, for example...

                dispatch_async(dispatch_get_main_queue(), ^{
                    // BTW, this is not on the main thread, so dispatch UI updates back to the main queue

                    [[[UIAlertView alloc] initWithTitle:nil message:@"This app requires access to your contacts to function properly. Please visit to the \"Privacy\" section in the iPhone Settings app." delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil] show];
                });
            }
        CFRelease(addressBook);
    });

}

Then list contact by this function

- (void)listPeopleInAddressBook:(ABAddressBookRef)addressBook
{
    NSArray *allPeople = CFBridgingRelease(ABAddressBookCopyArrayOfAllPeople(addressBook));
    numberOfPeople = [allPeople count];

    for (NSInteger i = 0; i < numberOfPeople; i++) {
        ABRecordRef person = (__bridge ABRecordRef)allPeople[i];

        NSString *firstName = CFBridgingRelease(ABRecordCopyValue(person, kABPersonFirstNameProperty));
        NSString *lastName  = CFBridgingRelease(ABRecordCopyValue(person, kABPersonLastNameProperty));
        NSLog(@"Name:%@ %@", firstName, lastName);
        if (firstName==nil) {
            firstName=@"";
        }

        [nm addObject:firstName];
        if (lastName==nil) {
            lastName=@"";
        }
        [ttl addObject:lastName];


        ABMultiValueRef phoneNumbers = ABRecordCopyValue(person, kABPersonPhoneProperty);

        // CFIndex numberOfPhoneNumbers = ABMultiValueGetCount(phoneNumbers);
        //for (CFIndex i = 0; i < numberOfPhoneNumbers; i++) {
        NSString *phoneNumber = CFBridgingRelease(ABMultiValueCopyValueAtIndex(phoneNumbers, 0));
        NSLog(@"  phone:%@", phoneNumber);
        if (phoneNumber==nil) {
            phoneNumber=@"";
        }
        [phn addObject:phoneNumber];
        // }
        // CFRelease(phoneNumbers);

        NSLog(@"=============================================");
    }
}
Nimit Parekh
  • 16,776
  • 8
  • 50
  • 72
tania_S
  • 1,350
  • 14
  • 23
  • I don't want to fetch all contacts in my ios application, I just want to fetch the contacts who are using the same application. – Aarti Oza Nov 16 '15 at 11:38
  • Then you have to take help of database where all users are saved and have to check which users match with your contacts. – tania_S Nov 16 '15 at 13:25
0

Its not possible without back-end, you can not do it within only ios application.

Aarti Oza
  • 1,134
  • 2
  • 14
  • 31