12

I am importing all the native address book contacts into my app. Currently, the alertview to ask for permission to 'allow access to the Contacts' is done at the launch of the app.

How do I change it so permission is asked elsewhere in the app? At the click of a button, just like Instagram?

See this picture:

enter image description here

Midhun MP
  • 103,496
  • 31
  • 153
  • 200
Hassan
  • 183
  • 1
  • 2
  • 7
  • Check [Programmatically Request Access to Contacts](http://stackoverflow.com/questions/12648244/programmatically-request-access-to-contacts) – Midhun MP Oct 12 '13 at 19:28
  • iOS 9 and above : http://stackoverflow.com/a/39374916/569789 – Zaraki Sep 07 '16 at 16:20

2 Answers2

22

You can read the documentation HERE.

Here is some code to get you started:

//First of all import the AddRessBookUI 
  #import <AddressBookUI/AddressBookUI.h>

  // Request to authorise the app to use addressbook
  ABAddressBookRef addressBookRef = ABAddressBookCreateWithOptions(nil, nil);

  if (ABAddressBookGetAuthorizationStatus() == kABAuthorizationStatusNotDetermined) {
    ABAddressBookRequestAccessWithCompletion(addressBookRef, ^(bool granted, CFErrorRef error) {
      if (granted) {
          // If the app is authorized to access the first time then add the contact
          [self _addContactToAddressBook];
      } else {
          // Show an alert here if user denies access telling that the contact cannot be added because you didn't allow it to access the contacts
      }
    });
  }
  else if (ABAddressBookGetAuthorizationStatus() == kABAuthorizationStatusAuthorized) {
    // If the user user has earlier provided the access, then add the contact
    [self _addContactToAddressBook];
  }
  else {
    // If the user user has NOT earlier provided the access, create an alert to tell the user to go to Settings app and allow access
  }

Here are another couple of tutorials that you might like to see that you can find HERE and HERE.

Hope this helps!

UPDATE: You have to use didFinishLaunchingWithOptions to detect the first time your app launches:

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
    if ([[NSUserDefaults standardUserDefaults] boolForKey:@"HasLaunchedOnce"])
    {
        // The app has already launched once
    }
    else
    {
        [[NSUserDefaults standardUserDefaults] setBool:YES forKey:@"HasLaunchedOnce"];
        [[NSUserDefaults standardUserDefaults] synchronize];
        // This is the first time the app is launched
    }
}
AJ112
  • 5,291
  • 7
  • 45
  • 60
  • Cool, how do I stop the app asking for access at the launch of the app? – Hassan Oct 12 '13 at 22:52
  • @Hassan check my update on how to detect the first time your app launches. In the else part, you can write some code to not ask for the contacts access. – AJ112 Oct 13 '13 at 09:48
0

App asking for permission when you try retrive contacts.

Just call retriving after button touch, not after launching.

AlKozin
  • 904
  • 8
  • 25