0

In my app I have to access the address book, so in iOS>6.0 I have to ask permission to the user. I do this:

ABAddressBookRef addressBook = ABAddressBookCreate();

if(floor(NSFoundationVersionNumber) >= NSFoundationVersionNumber_iOS_6_0)
{
    //iOS is >= 6.0
    if (ABAddressBookGetAuthorizationStatus() == kABAuthorizationStatusNotDetermined)
    {
        ABAddressBookRequestAccessWithCompletion(addressBook, ^(bool granted, CFErrorRef error)
                                                 {
                                                     if (granted)
                                                     {
                                                         [self showContacts:addressBook];
                                                     }
                                                 });


    }
    else if (ABAddressBookGetAuthorizationStatus() == kABAuthorizationStatusAuthorized)
    {
        [self showContacts:addressBook];
    }
    else
    {
        //showAccessDeniedAlert();
    }
}
else
{
    //iOS is < 6.0
    [self showContacts:addressBook];
}

In showContacts it's all alright, but: the first time I use the app on a device, it asks me if I want to let it access the AddressBook, i press "OK" and showContactsisn't called! So I have to close the app, restart it, and it works perfectly.

This is my showContacts method:

- (void) showContacts:(ABAddressBookRef)addressBook
{
    CFArrayRef allContacts = ABAddressBookCopyArrayOfAllPeople(addressBook);
    CFIndex numberOfContacts = ABAddressBookGetPersonCount(addressBook);

    for (int ii = 0; ii < numberOfContacts; ii++)
    {
        ABRecordRef contactRef = CFArrayGetValueAtIndex(allContacts, ii);
        ABMultiValueRef *phones = ABRecordCopyValue(contactRef, kABPersonPhoneProperty);
        for(CFIndex jj = 0; jj < ABMultiValueGetCount(phones); jj++)
        {
            CEPerson *person = [[CEPerson alloc] init];
            NSString *name = (__bridge_transfer NSString *)ABRecordCopyValue(contactRef,kABPersonFirstNameProperty);
            NSString *surname = (__bridge_transfer NSString *)ABRecordCopyValue(contactRef, kABPersonLastNameProperty);
            CFStringRef phoneNumberRef = ABMultiValueCopyValueAtIndex(phones, jj);
            NSString *phoneNumber = (__bridge NSString *)phoneNumberRef;

            person.name = name;
            person.surname = surname;
            person.telephone = phoneNumber;

            [arrContacts addObject:person];
        }
    }
    NSSortDescriptor *sort = [NSSortDescriptor sortDescriptorWithKey:@"surname" ascending:YES];
    [arrContacts sortUsingDescriptors:[NSArray arrayWithObject:sort]];
    [self selectAllContacts];
}

How can I make it work even right after the user presses "OK"?

Thank you all.

Ales
  • 19
  • 8

1 Answers1

0

I figured it out. I simply added

[tableView reloadData];

at the end of my showContacts method.

Ales
  • 19
  • 8