0

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?

Matthias Bauch
  • 89,811
  • 20
  • 225
  • 247
subharb
  • 3,374
  • 8
  • 41
  • 72

1 Answers1

1

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.

sergio
  • 68,819
  • 11
  • 102
  • 123
  • The problem is that the value of multi is different. So when I try to access values of that ABMultiValueRef it crashes because for some reason it's not returning the right object. – subharb Jan 06 '13 at 12:23