简体   繁体   中英

Strange behavior of iOS simulator and real device(contacts+nsdictionary)

Now i ve got this code:

-(void)getContacts{
ABAddressBookRef currentAddressBook = ABAddressBookCreate();
if (currentAddressBook) {
    CFArrayRef allBook = ABAddressBookCopyArrayOfAllPeople(currentAddressBook);
    if (allBook) {
        for (int index=0; index < CFArrayGetCount(allBook); index++){
            ABRecordRef currentPerson = CFArrayGetValueAtIndex(allBook, index);

            NSString *firstName = (__bridge NSString *)ABRecordCopyValue(currentPerson, kABPersonFirstNameProperty);
            NSString *lastName = (__bridge NSString *)ABRecordCopyValue(currentPerson, kABPersonLastNameProperty);
            NSData *imageData = (__bridge NSData*)ABPersonCopyImageDataWithFormat(currentPerson, kABPersonImageFormatThumbnail);  
            NSMutableArray *tempArrayForPhones = [[NSMutableArray alloc] init];

            ABMultiValueRef phoneNumbersMultiValue = ABRecordCopyValue(currentPerson, kABPersonPhoneProperty);
            for(CFIndex counter = 0; counter < ABMultiValueGetCount(phoneNumbersMultiValue); counter++){
                CFStringRef currentLabel = ABMultiValueCopyLabelAtIndex(phoneNumbersMultiValue, counter);
                NSString *phoneLabel =(__bridge NSString*) ABAddressBookCopyLocalizedLabel(currentLabel);

                CFStringRef phoneNumberRef = ABMultiValueCopyValueAtIndex(phoneNumbersMultiValue, counter);
                currentLabel = ABMultiValueCopyLabelAtIndex(phoneNumbersMultiValue, counter);
                phoneLabel =(__bridge NSString*) ABAddressBookCopyLocalizedLabel(currentLabel);
                NSString *phoneNumber = (__bridge NSString *)phoneNumberRef;
                CFRelease(phoneNumberRef);
                CFRelease(currentLabel);
                NSDictionary *tempDictForPhonew = [NSDictionary dictionaryWithObjectsAndKeys:phoneNumber,@"number",
                                                                                            phoneLabel,@"label",
                                                                                            nil];
                [tempArrayForPhones addObject:tempDictForPhonew];
            }
            NSDictionary *dictionaryWithAddressBook = [NSDictionary dictionaryWithObjectsAndKeys:firstName,@"firstName",
                                                                                                lastName,@"lastName",
                                                                                                imageData,@"image",
                                                                                                tempArrayForPhones,@"phones",
                                                                                                nil];
            tempArrayForPhones = nil;
            [dataArray addObject:dictionaryWithAddressBook];

            CFRelease(phoneNumbersMultiValue);
        }
        CFRelease(allBook);
    }
    CFRelease(currentAddressBook);
}
}

all works fine on simulator, i get array with dictionary, which contains all fields i need. But when i run code on real device(iOS 5.1) dictionary got only first name and last name. I tried to do some nslog around initializing dictionary, and temp dictionary with phones and image data existed, but not in final dataArray. Whats wrong?

Problem solved; It was a mistake in parsing address book; I forgot to handle contacts without first name.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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