IOS6.0调用通讯录和之前的差别

6、通讯录列表获取差异
自iOS6.0后获取通讯录列表需要询问用户,经过用户同意后才可以获取通讯录用户列表。而且ABAddressBookRef的初始化工作也由ABAddressBookCreate函数转变为ABAddressBookCreateWithOptions函数。下面代码是兼容之前版本的获取通讯录用户列表方法。
ABAddressBookRef addressBook = nil;
 
if ([[UIDevice currentDevice].systemVersion floatValue] >= 6.0)
{
    addressBook = ABAddressBookCreateWithOptions(NULL, NULL);
    //等待同意后向下执行
    dispatch_semaphore_t sema = dispatch_semaphore_create(0);
    ABAddressBookRequestAccessWithCompletion(addressBook, ^(bool granted, CFErrorRef error)
    {
        dispatch_semaphore_signal(sema);
    });
          
    dispatch_semaphore_wait(sema, DISPATCH_TIME_FOREVER);
    dispatch_release(sema);
}
else
{
    addressBook = ABAddressBookCreate();
}
 
NSArray *personArray = (NSArray *)ABAddressBookCopyArrayOfAllPeople(addressBook);

你可能感兴趣的:(iOS6)