应用程序正在崩溃访问iPhone的联系人书籍

时间:2015-09-02 08:58:08

标签: ios

当我点击联系人以访问iPhone的联系人时,我的应用程序崩溃了。我使用了以下代码。

-(NSString*)readContactBook{

    @try {
        NSString *jsonResponse;

        NSLog(@"Read Addressbook: %@", [self getTimeInMilliSeconds]);

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

            NSLog(@"Fetching Contacts from AB");
            ABRecordRef source = ABAddressBookCopyDefaultSource(addressBook);
            CFArrayRef allPeople = ABAddressBookCopyArrayOfAllPeopleInSourceWithSortOrdering(addressBook, source, kABPersonSortByFirstName);

            CFIndex nPeople = CFArrayGetCount(allPeople);

            //CFIndex nPeople = ABAddressBookGetPersonCount(addressBook);

            NSMutableArray* arrContacts = [[NSMutableArray alloc]init];

            NSLog(@"Total Contacts in device: %ld",nPeople);

            for (int i = 0; i < nPeople; i++)
            {
                ABRecordRef person = CFArrayGetValueAtIndex(allPeople, i);

                //get First Name and Last Name
                NSMutableArray *arrAllContacts = [[[NSMutableArray alloc]init] autorelease];

                NSString *strFirstName = (__bridge NSString*)ABRecordCopyValue(person, kABPersonFirstNameProperty);

                NSString *strLastName =  (__bridge NSString*)ABRecordCopyValue(person, kABPersonLastNameProperty);


                if (!strFirstName) {
                    strFirstName = @"";
                }
                if (!strLastName) {
                    strLastName = @"";
                }


                //get Phone Numbers

                NSMutableArray *phoneNumbers = [[NSMutableArray alloc] init];

                ABMultiValueRef multiPhones = ABRecordCopyValue(person, kABPersonPhoneProperty);
                for(CFIndex i=0;i<ABMultiValueGetCount(multiPhones);i++) {
                    NSMutableDictionary *dictContact = [[[NSMutableDictionary alloc]init] autorelease];

                    CFStringRef phoneNumberRef = ABMultiValueCopyValueAtIndex(multiPhones, i);
                    CFStringRef locLabel = ABMultiValueCopyLabelAtIndex(multiPhones, i);
                    NSString *phoneLabel =(__bridge NSString*) ABAddressBookCopyLocalizedLabel(locLabel);
                    if([phoneLabel isEqualToString:@""] || !phoneLabel) {
                        phoneLabel = @"phone";
                    }
                    NSString *phoneNumber = (__bridge NSString *) phoneNumberRef;
                    [phoneNumbers addObject:phoneNumber];

                    [dictContact setObject:phoneNumber forKey:phoneLabel];


                     CFRelease(phoneNumberRef);
                    CFRelease(locLabel);

在一个案例中,我评论CFRelease(phoneNumberRef);CFRelease(locLabel);应用在访问联系人时停止崩溃。但问题在2个月后重新开放。 请建议适当的修复。

0 个答案:

没有答案