0

我试图在我的联系人列表中进行搜索,但是如果它是从应用商店新执行的,或者正如我现在测试的那样,从 TestFlight 执行,则此代码会崩溃。如果我卸载应用程序并点击运行,它会完美运行。但是直接从 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);上,当它的 Fresh from TestFlight “address: Denis” 是一个联系人时,它会在这一行打印,从 Xcode 执行它会打印,“address: ABMultiValueRef 0x1fb68400 with 1 value(s) ...”,所以我看到了问题所在,即不同,我不明白为什么不同,你能告诉我为什么吗?

4

1 回答 1

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

希望这可以帮助。

于 2013-01-04T18:14:08.587 回答