简体   繁体   English

在iOS8中获取联系人列表,导致应用崩溃

[英]Get contacts list in iOS8, crashes the app

Getting contacts in iOS8 (iPhone 5s) causes crash randomly. 在iOS8(iPhone 5s)中获取联系人会导致随机崩溃。 Crash details: http://hastebin.com/ukihinucaf.md 崩溃详细信息: http : //hastebin.com/ukihinucaf.md

At line: 在行:

    ABMultiValueRef multiEmails = ABRecordCopyValue(person, kABPersonEmailProperty);

Here is the whole function: 这是整个功能:

-(NSArray *) getAllContacts
{

    CFErrorRef *error = nil;

    ABAddressBookRef addressBook = ABAddressBookCreateWithOptions(NULL, error);

    __block BOOL accessGranted = NO;
    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);

    }
    else {
        accessGranted = YES;
    }

    if (accessGranted) {

        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++)
        {

            ABRecordRef person = CFArrayGetValueAtIndex(allPeople, i);

            //get Contact email

            ABMultiValueRef multiEmails = ABRecordCopyValue(person, kABPersonEmailProperty);

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

                [items addObject:contactEmail];
            }

        }
        return items;

    } else {
        NSLog(@"Cannot fetch Contacts :( ");
        UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Fetch failed" message:@"Can't fetch your contacts." delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil, nil];
        [alert show];
        return nil;

    }
}

Anybody knows what can be a problem? 有人知道会有什么问题吗?

You are retrieving ABAddressBookGetPersonCount (the count of all the people in all of the sources, not just the count of people in the default source), and using that as the upper threshold when you're iterating through the array. 您正在检索ABAddressBookGetPersonCount (所有源中的所有人员的数量,而不仅仅是默认源中的人员的数量),并在遍历数组时将其用作上限。 But the array doesn't contain all of the people in the address book, but rather only those in the default source. 但是数组并不包含通讯录中的所有人员,而只包含默认源中的人员。 Thus, you may exceed the number of items in the array. 因此,您可能会超出数组中的项目数。

I would suggest using CFArrayGetCount rather than ABAddressBookGetPersonCount . 我建议使用CFArrayGetCount而不是ABAddressBookGetPersonCount

ABAddressBookCreateWithOptions(NULL, error ); ABAddressBookCreateWithOptions(NULL, error );

Should be this 应该是这个

CFErrorRef error = NULL;
ABAddressBookCreateWithOptions(NULL, &error);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM