在我的应用程序中访问iCloud联系人时会导致iOS崩溃

时间:2014-02-28 08:18:46

标签: ios objective-c ios7 abaddressbook

我想在我的应用中访问我的地址簿联系人,我能够成功地从我的地址簿中检索所有联系人,但如果在我的iPad中同步我的i-cloud帐户,那么我的地址簿会从我的i-cloud更新联系人,如果现在我访问我的联系人,它会导致崩溃。 请帮助我,我完全陷入困境,不知道该怎么做。 我可以轻松访问地址簿联系人,但是一旦它从i-cloud同步,然后我获取地址簿,就会导致崩溃并给我带来错误的超额错误。

以下是我用来获取联系人的代码。

+(NSArray *)getAllContacts
{

    CFErrorRef *error = nil;


    ABAddressBookRef addressBook = ABAddressBookCreateWithOptions(NULL, error);

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

    if (accessGranted) {

#ifdef DEBUG
        NSLog(@"Fetching contact info ----> ");
#endif


        ABAddressBookRef addressBook = ABAddressBookCreateWithOptions(NULL, error);
        ABRecordRef source = ABAddressBookCopyDefaultSource(addressBook);
        CFArrayRef allPeople = ABAddressBookCopyArrayOfAllPeopleInSourceWithSortOrdering(addressBook, source, kABPersonSortByFirstName);
        CFIndex nPeople = ABAddressBookGetPersonCount(addressBook);
        NSMutableArray* items = [NSMutableArray arrayWithCapacity:nPeople];


        for (int i = 0; i < nPeople; i++)
        {
            MContact *contacts = [MContact new];

            ABRecordRef person = CFArrayGetValueAtIndex(allPeople, i);

            //get First Name and Last Name

            contacts.firstName = (__bridge NSString*)ABRecordCopyValue(person, kABPersonFirstNameProperty);

            contacts.lastName =  (__bridge NSString*)ABRecordCopyValue(person, kABPersonLastNameProperty);

            if (!contacts.firstName) {
                contacts.firstName = @"";
            }
            if (!contacts.lastName) {
                contacts.lastName = @"";
            }


            NSMutableArray *contactEmails = [NSMutableArray new];
            ABMultiValueRef multiEmails = ABRecordCopyValue(person, kABPersonEmailProperty);

            for (CFIndex i=0; i<ABMultiValueGetCount(multiEmails); i++) {
                CFStringRef contactEmailRef = ABMultiValueCopyValueAtIndex(multiEmails, i);
                NSString *contactEmail = (__bridge NSString *)contactEmailRef;

                [contactEmails addObject:contactEmail];
                // NSLog(@"All emails are:%@", contactEmails);

            }

            if([contactEmails count]==0){

            }
            else{
                [contacts setemails:contactEmails];
                [items addObject:contacts];

            }




#ifdef DEBUG
#endif




        }
        return items;



    } else {
#ifdef DEBUG
        NSLog(@"Cannot fetch Contacts :( ");
#endif
        return NO;


    }


}

2 个答案:

答案 0 :(得分:0)

您是否在代码中确切地找到了崩溃附加的位置?如果它在你的“for(int i = 0; i&lt; nPeople; i ++)”循环中,可能只有1个联系人有问题......

PS 1,你应该删除第7行:.... ABAddressBookCreateWithOptions,因为你需要先检查授权....

PS 2,你真的只需要默认来源的联系人吗?如果用户还有来自Lotus Notes或其他gmail的联系人呢....

答案 1 :(得分:0)

我看到一个问题,但不确定它会解决您的崩溃......您应该用 <替换 _ 网桥 / em> _bridge_transfer

contacts.firstName = (__bridge_transfer NSString*)ABRecordCopyValue(person,kABPersonFirstNameProperty);

为姓氏做同样的事情......

如果它没有解决崩溃,我建议尝试在你的AB女巫崩溃中隔离“人”,并尝试在你的“for循环”中跳过它。这应该有助于我们知道这个人是否只是一个问题,或者是否与要加载的人数有关。在我的ABLocation App中,我知道有些用户有> 500个联系人,他们没有PB ......