ABRecordCopyValue有不同的值?

时间:2013-01-04 18:04:50

标签: ios abaddressbook

我试图在我的联系人列表中进行搜索,但如果它是从应用程序商店新执行的,或者正如我现在测试的那样,这个代码会从TestFlight中崩溃。 如果我卸载应用程序并点击Run它就完美了。但是从TestFlight执行它崩溃了,崩溃日志声明并且它在我

的行中失败了
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++;

}

在这一行NSLog(@"address: %@", multi);上,它打印出来自TestFlight的Fresh“地址:Denis”,它是一个联系人,从它打印的Xcode执行,“地址:ABMultiValueRef 0x1fb68400有1个值...”,所以我看到这里的问题,那个差异,我不明白为什么会有所不同,你能告诉我为什么吗?

1 个答案:

答案 0 :(得分:1)

您似乎没有正确访问地址簿数据。我明白,正确的方法是:

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>
}

希望这有帮助。