Question

Whenever the user presses OK on the AddressBook permission UIAlertView everything goes fine except for a delay in executing these commands, weirdly it takes about 5 seconds for these commands to be executed, even though the NSLog is printed instantly.

The same code works instantly in Calendar permission.

Can someone please help me here? thank you.

The code which is being executed after a delay(5 secs)

   NSLog(@"Granted!");
 _1234.backgroundColor = [UIColor lightGrayColor];
 [_12345 setHidden:NO];
 [ _qwerty setTitleColor:[UIColor lightGrayColor] forState:UIControlStateNormal];
 [_qwerty1 setHidden:YES];

Complete function

ABAddressBookRef addressBookRef = ABAddressBookCreateWithOptions(NULL, NULL);

if (ABAddressBookGetAuthorizationStatus() == kABAuthorizationStatusNotDetermined) {

ABAddressBookRequestAccessWithCompletion(addressBookRef, ^(bool granted, CFErrorRef error) {

if (error)

        {
            // display error message here
        }

else if (!granted)

        {
            // display access denied error message here

        }
        else
        {
            NSLog(@"Granted!"); //this gets printed instantly

            _qwerty.userInteractionEnabled = NO;

            _1234.backgroundColor = [UIColor lightGrayColor];
            [_12345 setHidden:NO];
            [ _qwerty setTitleColor:[UIColor lightGrayColor] forState:UIControlStateNormal];
            [_qwerty1 setHidden:YES];

            [self qwerty4];
        }



    });
}
Was it helpful?

Solution

your delay is probably because the completion block is not executed in the main thread, and all UI operation must be executed in the main thread, dispatch your UI code in the main thread like this:

dispatch_async(dispatch_get_main_queue(), ^{

    NSLog(@"Granted!"); //this gets printed instantly

    _qwerty.userInteractionEnabled = NO;

    _1234.backgroundColor = [UIColor lightGrayColor];
    [_12345 setHidden:NO];
    [ _qwerty setTitleColor:[UIColor lightGrayColor] forState:UIControlStateNormal];
    [_qwerty1 setHidden:YES];

    [self qwerty4];
});
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top