-1

I want to link the address book view controller in my app , I have searched the internet and found in apple developer guide but they show how to open contacts picker when clicking on a button and I don't want that I want the contacts picker be a view controller in my storyboard . I think i can do that by making a custom tableviewcontroller and getting information from address book or can I do it directly ?

Coder404
  • 742
  • 2
  • 7
  • 21

1 Answers1

2

This should help you get started:

Also, don't forget to import and link to the following frameworks:

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

Simply call: [self requestPermissionForContacts];

- (void)requestPermissionForContacts
{
    ABAddressBookRef addressBookRef = ABAddressBookCreateWithOptions(NULL, NULL);

    if (ABAddressBookGetAuthorizationStatus() == kABAuthorizationStatusNotDetermined)
    {
        ABAddressBookRequestAccessWithCompletion(addressBookRef, ^(bool granted, CFErrorRef error)
        {
            if (granted)
            {
                dispatch_async(dispatch_get_main_queue(), ^
                {
                    [self findContacts];
                });
            }
            else
            {
                dispatch_async(dispatch_get_main_queue(), ^
                {
                    UIAlertView *alert = [[UIAlertView alloc]initWithTitle:@"Find Contacts" message:@"To allow us to find your contacts, you will need to go to the Settings app > Privacy > Contacts, and set your app name here to On." delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil];
        [alert show];
                });
            }
        });
    }
    else if (ABAddressBookGetAuthorizationStatus() == kABAuthorizationStatusAuthorized)
    {
        [self findContacts];
    }
    else
    {
        UIAlertView *alert = [[UIAlertView alloc]initWithTitle:@"Find Contacts" message:@"To allow us to find your contacts, you will need to go to the Settings app > Privacy > Contacts, and set your app name here to On." delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil];
        [alert show];
    }
}

- (void)findContacts
{
    ABAddressBookRef addressBookRef = ABAddressBookCreateWithOptions(NULL, NULL);

    CFArrayRef people = ABAddressBookCopyArrayOfAllPeople(addressBookRef);

    CFIndex numberOfPeople = CFArrayGetCount(people);

    if (numberOfPeople > 0)
    {
        NSMutableArray *mutableEmailsArray = [[NSMutableArray alloc]init];
        NSMutableArray *mutablePhonesArray = [[NSMutableArray alloc]init];

        for (int i = 0; i < numberOfPeople; i++)
        {
            ABRecordRef ref = CFArrayGetValueAtIndex(people, i);

            ABMultiValueRef emails = (__bridge ABMultiValueRef)((__bridge NSString*)ABRecordCopyValue(ref, kABPersonEmailProperty));

            ABMultiValueRef phones = (__bridge ABMultiValueRef)((__bridge NSString*)ABRecordCopyValue(ref, kABPersonPhoneProperty));

            if (ABMultiValueGetCount(emails) > 0)
            {
                [mutableEmailsArray addObjectsFromArray:((__bridge NSArray *)(ABMultiValueCopyArrayOfAllValues(emails)))];
            }

            if (ABMultiValueGetCount(phones) > 0)
            {
                [mutablePhonesArray addObjectsFromArray:((__bridge NSArray *)(ABMultiValueCopyArrayOfAllValues(phones)))];
            }
        }

        UIAlertView *alert = [[UIAlertView alloc]initWithTitle:nil message:[NSString stringWithFormat:@"%ld Emails %ld Phone Numbers", (unsigned long)mutableEmailsArray.count, (unsigned long)mutablePhonesArray.count] delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil];
        [alert show];
    }
    else
    {
        UIAlertView *alert = [[UIAlertView alloc]initWithTitle:@"No Contacts" message:@"No contacts were found on your device." delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil];
        [alert show];
    }
}
klcjr89
  • 5,862
  • 10
  • 58
  • 91