subharb
subharb

Reputation: 3472

ABRecordCopyValue different values?

Im trying to search within my contact list, but this codes crashes if it's executed freshly from the app store, or, as Im testing now, from TestFlight. If I uninstall the app and hit Run it goes perfectly. But executed right from TestFlight it crashes, the crash log states and it fails in the line where i

BOOL found = NO;
NSString *name;
int i = 0;
NSLog(@"Hay %i", [people count]);
while (!found && i < [people count]) {
    ABRecordRef person = (ABRecordRef)[people objectAtIndex:i];
    ABMultiValueRef multi = ABRecordCopyValue(person, kABPersonPhoneProperty);
    NSLog(@"address: %@", multi);
    //Freshly from TestFlight this prints "address: Denis" wich is a contac, executed from Xcode it prints, address: ABMultiValueRef 0x1fb68400 with 1 value(s), so I see here's the problem

    if([[(NSMutableString*)ABMultiValueCopyValueAtIndex(multi, 0) componentsSeparatedByCharactersInSet:[[NSCharacterSet decimalDigitCharacterSet] invertedSet]] componentsJoinedByString:@""]){

        NSMutableString *tempPhone = [[NSMutableString alloc]initWithString:[[(NSMutableString*)ABMultiValueCopyValueAtIndex(multi, 0) componentsSeparatedByCharactersInSet:[[NSCharacterSet decimalDigitCharacterSet] invertedSet]] componentsJoinedByString:@""]];

        NSLog(@"telf: %@", tempPhone);

        int length = [tempPhone length] - 9;
        NSString *tempPhone2;
        if(length >= 0){
            tempPhone2 = [[NSString alloc]initWithString:[tempPhone substringFromIndex:length]];
        }

        NSLog(@"el telf: %@ y nombre %@ int %i",tempPhone2, [NSString stringWithFormat:@"%@ %@",ABRecordCopyValue(person, kABPersonFirstNameProperty) ? (NSString *)ABRecordCopyValue(person, kABPersonFirstNameProperty) : @"",ABRecordCopyValue(person, kABPersonLastNameProperty) ? (NSString *)ABRecordCopyValue(person, kABPersonLastNameProperty) : @""], i);

        if([[key objectForKey:@"phone"] isEqualToString:tempPhone2]){

            name = [NSString stringWithFormat:@"%@ %@",ABRecordCopyValue(person, kABPersonFirstNameProperty) ? (NSString *)ABRecordCopyValue(person, kABPersonFirstNameProperty) : @"",ABRecordCopyValue(person, kABPersonLastNameProperty) ? (NSString *)ABRecordCopyValue(person, kABPersonLastNameProperty) : @""];

            found = YES;

        }
    }

    i++;

}

On this line NSLog(@"address: %@", multi); it prints when its Fresh from TestFlight "address: Denis" wich is a contact, executed from Xcode it prints, "address: ABMultiValueRef 0x1fb68400 with 1 value(s)...", so I see here's the problem, that difference, what I dont understand is why is different, can you tell me why?

Upvotes: 0

Views: 370

Answers (1)

sergio
sergio

Reputation: 69037

It seems that you are not accessing the address book data correctly. I understand, the right way would be something like:

ABMultiValueRef multi = ABRecordCopyValue(person, kABPersonPhoneProperty);
NSString* label;
for (int i = 0; i < ABMultiValueGetCount(multi); i++) {
    label = (NSString*)ABMultiValueCopyLabelAtIndex(multi, i);
    ... <DO YOUR PROCESSING on label>
}

Hope this helps.

Upvotes: 1

Related Questions