0

Hi I'm currently working on a project for a customer where i need to access the phones contacts.

I managed to ask for the Permission to access the contacts and i m handling the two different states (granted, denied). Apparently the customer wants the following workflow:

  • hit an add button
  • ask for permission
  • granted: performs a segue to a tablewview with all contacts listed
  • denied: performs a segue to a differnt view and keeps asking on the inital button hit to grant access to the contacs

I ve managed the complete flow and fetched the complete data. No i m stuck with two problems:

  1. I can't get the the ask for permission alertview pop up again (from my understanding the user needs to set this in the Application Settings ->App privacy settings). Is this correct?
  2. It appears that if access is granted for the first time and i perform a segue the tableview is empty because the data array is nil (i can't figure out why).
- (void)addButtonTouched {
[self.addressBook accessAddressBook];
[self.activityView startActivityViewWithMessage:@"Wait a second"];
if (access) {
    self.contactsArray = [self.addressBook getAllContacts];
    if (self.contactsArray.count != 0) {
        [self performSegueWithIdentifier:@"addEntrySegue" sender:self];
    } else {
        [self performSegueWithIdentifier:@"noContactsSegue" sender:self];
    }
} 

Am I pushing to soon to the next ViewController to fill self.contactsArray?

My other approach was to send a Notifictaion to my rootViewController when the access was granted and then perform the segue. This was the closest result i could get, but the ViewController push delayed aber 8-10 seconds.

>     - (void)receivedNotification:(NSNotification *)notification {
>         if (access) {
>             self.contactsArray = [self.addressBook getAllContacts];
>             if (self.contactsArray.count != 0) {
>                 [self performSegueWithIdentifier:@"addEntryrSegue" sender:self];
>             } else {
>                 [self performSegueWithIdentifier:@"noContactsSegue" sender:self];
>             }
>         }
>     }

Thanks in advance. I hope i got this explained well enough.

d3p0nit
  • 442
  • 5
  • 16

1 Answers1

0
  1. Yes. The user has to re-enable it manually from the setting. Your best bet may be to create the alternate view with instructions on how to accept it.

  2. The array is filled with all the contacts. You want to make sure that you do two things. One: reloadData() on the table. Two: make sure you handle asynchronous operation correctly. So, the best way to handle this is running the code like this:

    dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0ul);
    dispatch_async(queue, ^{
        // Fill array with contacts here. Run the function. Whatever you need to do goes here.
                dispatch_sync(dispatch_get_main_queue(), ^{
                    // Reload the table. Whatever UI changes you want go here.
                });
    });