iOS应用程序在后台崩溃,因为更改了设置 – >隐私 – >联系我的应用程序开启/关闭

在我的应用程序中我收到联系信息直接购买这样做…
ABAddressBookRef m_addressbook = ABAddressBookCreate();

CFArrayRef allPeople = ABAddressBookCopyArrayOfAllPeople(m_addressbook);

CFIndex nPeople = ABAddressBookGetPersonCount(m_addressbook);

for (int i=0;i < nPeople;i++)
{
    ABRecordRef ref = CFArrayGetValueAtIndex(allPeople,i);
    CFStringRef company,firstName,lastName;

     firstName = ABRecordCopyValue(ref,kABPersonFirstNameProperty);
     lastName  = ABRecordCopyValue(ref,kABPersonLastNameProperty);
     company = ABRecordCopyValue(ref,kABPersonOrganizationProperty);
}

所以,我需要先检查这是否是开/关设置 – >隐私 – >我的应用程序的联系人ON / OFF.

为此,我这样做:

__block BOOL accessGranted = NO;


float sysver = [[[UIDevice currentDevice]systemVersion]floatValue];

if(sysver>=6) {
    ABAddressBookRef addressBook = ABAddressBookCreate();

    if (ABAddressBookRequestAccessWithCompletion != NULL) {  
        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);
     dispatch_release(sema);
     } else { 
         accessGranted = YES;
     }
} else {
    accessGranted = YES;
}

if(accessGranted) 
{
    // doing my stuff and moving on
} else {
    // please make Settings --> Privacy --> Contacts  my app.  ON for accessing contacts.
}

我的问题是,在设备上安装应用程序后第一次,应用程序要求我提供“不允许”/“确定”联系人授予访问权限的警报.我点击确定但设置 – >隐私 – >我的应用程序的联系人是关闭所以再次得到警报,使其打开,“设置”“确定”所以选择设置,我把它打开,一旦我使它在应用程序得到SIGKILL没有控制台.

以后每当我将隐私设置改为OFF到ON应用程序在后台崩溃时.我没有控制台SIGKILL.

提前致谢.

解决方法

还有另一篇文章发现类似问题 here.

它是一种OS功能,当更改隐私设置时,每个应用程序都会终止.这是为了确保每个应用程序都遵守用户隐私,并且在更改隐私设置后不会继续使用任何缓存数据.

另请注意您的建议代码

float sysver = [[[UIDevice currentDevice]systemVersion]floatValue];
    if(sysver>=6) {

不推荐使用Apple,并且有更好的,更官方的方法,例如使用Foundation #define值,即

BOOL isiOS6OrMoreRecent = NSFoundationVersionNumber >= NSFoundationVersionNumber_iOS_6_0 ? YES : NO;

但是,使用iOS版本来确定可用功能是非常糟糕的做法,而是独立于操作系统版本检查功能本身(通讯簿代码here,例如:

if (ABAddressBookRequestAccessWithCompletion) { // if in iOS 6
    // Request authorization to Address Book
    ABAddressBookRef addressBookRef = ABAddressBookCreateWithOptions(NULL,NULL);

相关文章

当我们远离最新的 iOS 16 更新版本时,我们听到了困扰 Apple...
欧版/美版 特别说一下,美版选错了 可能会永久丧失4G,不过只...
一般在接外包的时候, 通常第三方需要安装你的app进行测...
前言为了让更多的人永远记住12月13日,各大厂都在这一天将应...