Как дать разрешение на доступ к адресной книге телефона в ios 7?

NSMutableArray *arrContacts=[[NSMutableArray alloc]init];


    CFErrorRef *error = nil;

    ABAddressBookRef addressbook = ABAddressBookCreateWithOptions(NULL, error);

    __block BOOL accessGranted = NO;

    if (ABAddressBookRequestAccessWithCompletion != NULL) { // we are used on the iOS 6
        dispatch_semaphore_t sema = dispatch_semaphore_create(0);
        dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
            @autoreleasepool {
                // Write your code here...
                // Fetch data from SQLite DB
            }
        });

        ABAddressBookRequestAccessWithCompletion(addressbook, ^(bool granted, CFErrorRef error) {
            accessGranted = granted;
            dispatch_semaphore_signal(sema);
        });
        dispatch_semaphore_wait(sema, DISPATCH_TIME_FOREVER);
    }
    else { // we're on iOS 5 or older
        accessGranted = YES;
    }
    if (accessGranted)
    {

    }

Вышеупомянутый код отлично работает в симуляторе, но когда я запускаю приложение на Iphone 5c, он не спрашивает у меня разрешения, и я не могу получить номера телефонов из адресной книги устройства.

помогите мне решить эту проблему. Заранее спасибо.


person Nirav Patadiya    schedule 12.05.2014    source источник
comment
Попробуйте удалить приложение со своего телефона и снова установить.   -  person sbarow    schedule 12.05.2014


Ответы (1)


вызовите эту строку в своем viewDidLoad методе

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

        } 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

}
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
}
person Anbu.Karthik    schedule 12.05.2014
comment
Он работает нормально после удаления приложения с устройства и его повторной установки. - person Nirav Patadiya; 12.05.2014
comment
о, кк, прекрасный друг, я использовал этот метод выше, он отлично работает для установки приложения каждый раз - person Anbu.Karthik; 12.05.2014