1

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];
        }



    });
}
4

1 回答 1

1

您的延迟可能是因为完成块没有在主线程中执行,并且所有 UI 操作都必须在主线程中执行,在主线程中调度您的 UI 代码如下:

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];
});
于 2013-10-26T09:58:16.730 回答