ABAddressBookRequestAccessWithCompletion iOS 7和信号灯 [英] ABAddressBookRequestAccessWithCompletion iOS 7 and semaphores

查看:97
本文介绍了ABAddressBookRequestAccessWithCompletion iOS 7和信号灯的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

根据我的收集,此代码之前已发布过,并且也已使用过。我处于一种情况,需要知道我是否可以访问联系人之前,代码无法继续。

this code has been posted before, and been used as well, from what i could gather. i'm in a situation where i need the code to NOT continue until i know if i have access to the contacts.

可以正常工作。在iOS 7上,它会永远挂起,然后当我杀死该应用程序时,会出现对话框,要求允许访问联系人。

on Xcode 5.0.2 and iOS 6, this works just fine. on iOS 7, it hangs forever, and then when i kill the app the dialog box comes up asking to allow access to the contacts.

ABAddressBookRef addressBook = ABAddressBookCreate();

__block BOOL accessGranted = NO;

if (ABAddressBookRequestAccessWithCompletion != NULL) { // we're on iOS 6
dispatch_semaphore_t sema = dispatch_semaphore_create(0);

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

在尝试玩这个游戏时,我只是将BOOL设置为NO ,然后在块中将其设置为YES。块之后,我放了一个while循环,检查变量是否为YES,然后睡了1秒钟。在6、7上工作正常,在7上我永远都不会到达NSLog语句,而且我永远被困在while循环中打印日志语句。

in trying to play with this, i then simply set a BOOL to be NO, and then set it to YES within the block. after the block, i put a while loop that checked for the variable being YES, then slept for 1 second. works perfectly fine on 6, on 7 i never reach the NSLog statement in the block, and i'm stuck forever in the while loop printing the log statement.

am i在这里真的很really脚?还是这种方法在7时就被淘汰了?

am i doing something really lame here? or is this method gone haywire on 7?

ABAddressBookRef addressBook = ABAddressBookCreateWithOptions(NULL, &error);
ABAddressBookRequestAccessWithCompletion(addressBook, ^(bool granted, CFErrorRef error) {
    if (granted)
    {
        self.myAddressBook = addressBook;
    }

    done = YES;
    NSLog(@"in block, done is %@", done ? @"YES" : @"NO");
    didGrant = granted;

    //dispatch_semaphore_signal(sema);
});

while (!done)
{
    NSLog(@"done is %@", done ? @"YES" : @"NO");
    sleep(1);
}


推荐答案

我遇到了同样的问题,我意识到请求访问联系人的对话框仍然会阻止该应用程序,因此可能存在死锁。因此,我只是放弃了信号灯,并执行了以下操作(在iOS 7.1.1上测试并运行):

I had the same problem, and I realised that the Dialog box that requests access to the contacts blocks the app anyways, so maybe there's a deadlock. So I just ditched the semaphores and did something like this (tested and works on iOS 7.1.1):

ABAddressBookRef addressBook = ABAddressBookCreate();

MyController * __weak weakSelf = self;

if (ABAddressBookRequestAccessWithCompletion != NULL)
{ // we're on iOS 6
    if (ABAddressBookGetAuthorizationStatus() == kABAuthorizationStatusNotDetermined)
    {
        ABAddressBookRequestAccessWithCompletion(addressBook, ^(bool granted, CFErrorRef error) {
            dispatch_async(dispatch_get_main_queue(), ^{
                [weakSelf accessGrantedForAddressBook];
            });
        });    
    }
    else
        if (ABAddressBookGetAuthorizationStatus() == kABAuthorizationStatusAuthorized); //Maybe add something here to handle kABAuthorizationStatusRestricted
            [self accessGrantedForAddressBook];
    }
}
else // we're on iOS 5 or older
     [self accessGrantedForAddressBook];

这与Apple在其文档(搜索ABAddressBookRequestAccessWithCompletion)。此外,ABAddressBookRequestAccessWithCompletion异步并等待它有什么意义(请参见此处)。.

which is quite similar to what Apple does in their documentation (search for ABAddressBookRequestAccessWithCompletion). Besides, what's the point of ABAddressBookRequestAccessWithCompletion being asynchronous and waiting for it (see here)..

这篇关于ABAddressBookRequestAccessWithCompletion iOS 7和信号灯的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆