Подтвердить что ты не робот

Запросить разрешение на доступ к контактам iOS

Я импортирую все контакты адресной книги в свое приложение. В настоящее время предупреждение о запросе разрешения "разрешить доступ к контактам" выполняется при запуске приложения.

Как мне изменить его, чтобы разрешить разрешение в другом месте приложения? Одним нажатием кнопки, как Instagram?

Смотрите это изображение:

enter image description here

4b9b3361

Ответ 1

Вы можете прочитать документацию ЗДЕСЬ.

Вот вам какой код, чтобы вы начали:

//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
  }

Вот еще несколько руководств, которые вы могли бы увидеть, что вы можете найти ЗДЕСЬ и ЗДЕСЬ,

Надеюсь, это поможет!

ОБНОВЛЕНИЕ: Вы должны использовать didFinishLaunchingWithOptions для обнаружения при первом запуске приложения:

- (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
    }
}

Ответ 2

Приложение запрашивает разрешение при попытке восстановить контакты.

Просто вызовите восстановление после нажатия кнопки, а не после запуска.