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

相关文章

UITabBarController 是 iOS 中用于管理和显示选项卡界面的一...
UITableView的重用机制避免了频繁创建和销毁单元格的开销,使...
Objective-C中,类的实例变量(instance variables)和属性(...
从内存管理的角度来看,block可以作为方法的传入参数是因为b...
WKWebView 是 iOS 开发中用于显示网页内容的组件,它是在 iO...
OC中常用的多线程编程技术: 1. NSThread NSThread是Objecti...