0

I want to access all contacts from phonebook in IOS. I have tried below code

    CFErrorRef error = NULL;

    ABAddressBookRef addressBook = ABAddressBookCreateWithOptions(NULL, &error);

    if (addressBook != nil)
    {
        NSLog(@"Succesful.");

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

    }

this code is working good and all contacts get stored in "allContacts" array in my demo project but when i put this code in my existing project its returning "nil" record. Below is my header and implementation files of my existing project in which actually i want to use it.

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

@interface TestingViewController : UIViewController
-(IBAction)GetContacts;
-(void)GetPBAccess;
@end


    //.m file
    #import "TestingViewController.h"
    #import <AddressBook/AddressBook.h>
    #import <AddressBookUI/AddressBookUI.h>

    @interface TestingViewController ()

    @end

    @implementation TestingViewController

    - (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
    {
        self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
        if (self) {
            // Custom initialization
        }
        return self;
    }

    - (void)viewDidLoad
    {
        [super viewDidLoad];
        // Do any additional setup after loading the view.
        [self GetPBAccess];
    }
    - (void)didReceiveMemoryWarning
    {
        [super didReceiveMemoryWarning];
        // Dispose of any resources that can be recreated.
    }
    -(void)GetPBAccess
{
    ABAddressBookRef addressBook1 = ABAddressBookCreateWithOptions(NULL, NULL);
    switch (ABAddressBookGetAuthorizationStatus()) {
        case kABAuthorizationStatusNotDetermined:
        {
            ABAddressBookRequestAccessWithCompletion(addressBook1, ^(bool granted, CFErrorRef error) {
                if (granted) {
                    NSLog(@"Access Granted");
                    [self GetContacts];
                }
                else{
                    NSLog(@"Access Not Granted");
                }
            });
            break;
        }
        case kABAuthorizationStatusAuthorized:
        {
            NSLog(@"AUTHORIZATION ALREADY Granted");
            [self GetContacts];
            break;
        }
        case kABAuthorizationStatusDenied:
        {
            NSLog(@"AUTHORIZATION DENIED");
            break;
        }

        default:
            break;
    }
}


    -(IBAction)GetContacts
    {
        CFErrorRef error = NULL;

        ABAddressBookRef addressBook = ABAddressBookCreateWithOptions(NULL, &error);

        if (addressBook != nil)
        {
            NSLog(@"Succesful.");

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

            NSUInteger i = 0;
            for (i = 0; i < [allContacts count]; i++)
            {
                //Person *person = [[Person alloc] init];

                ABRecordRef contactPerson = (__bridge ABRecordRef)allContacts[i];

                NSString *firstName = (__bridge_transfer NSString *)ABRecordCopyValue(contactPerson, kABPersonFirstNameProperty);
                NSString *lastName =  (__bridge_transfer NSString *)ABRecordCopyValue(contactPerson, kABPersonLastNameProperty);
                NSString *fullName = [NSString stringWithFormat:@"%@ %@", firstName, lastName];

                //            person.firstName = firstName;
                //            person.lastName = lastName;
                //            person.fullName = fullName;

                //email
                ABMultiValueRef emails = ABRecordCopyValue(contactPerson, kABPersonEmailProperty);

                //            NSUInteger j = 0;
                //            for (j = 0; j < ABMultiValueGetCount(emails); j++)
                //            {
                //                NSString *email = (__bridge_transfer NSString *)ABMultiValueCopyValueAtIndex(emails, j);
                //                if (j == 0)
                //                {
                //                    person.homeEmail = email;
                //                    NSLog(@"person.homeEmail = %@ ", person.homeEmail);
                //                }
                //
                //                else if (j==1)
                //                    person.workEmail = email;
                //            }
                //
                //            [self.tableData addObject:person];
            }
        }

        CFRelease(addressBook);
    }


    /*
    #pragma mark - Navigation

    // In a storyboard-based application, you will often want to do a little preparation before navigation
    - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
    {
        // Get the new view controller using [segue destinationViewController].
        // Pass the selected object to the new view controller.
    }
    */

    @end
Vineet Choudhary
  • 7,433
  • 4
  • 45
  • 72

1 Answers1

0

The problem looks to be because you call

    [self GetPBAccess];
    [self GetContacts];

directly inline. What you should do is move the GetContacts call into the GetPBAccess so that it's called once you know you have access (and not called if you don't have access).

At the moment it will run before the user has granted access, certainly the first time you try it, and return nothing.

Wain
  • 118,658
  • 15
  • 128
  • 151