0

我是 iphone App Development(使用 iOS6)的新手,并且在将手机号码从联系人列表中提取到 UITableViewController 时遇到了问题。我可以正确获取名字和姓氏,但电话号码返回为空。我无法理解这背后的原因。我做错了什么?我的代码如下:

NSMutableArray *people = (__bridge_transfer NSMutableArray *) ABAddressBookCopyArrayOfAllPeople (addressBookRef);

        NSString *firstName = (__bridge_transfer NSString *)ABRecordCopyValue((__bridge ABRecordRef)([people objectAtIndex:indexPath.row]), kABPersonFirstNameProperty);

    NSString *lastName = (__bridge_transfer NSString *)ABRecordCopyValue((__bridge ABRecordRef)([people objectAtIndex:indexPath.row]), kABPersonLastNameProperty);

    ABMultiValueRef phoneNumbers = ABRecordCopyValue((__bridge ABRecordRef)([people objectAtIndex:indexPath.row]),kABPersonPhoneProperty);

if (([firstName isEqualToString:@""] || [firstName isEqualToString:@"(null)"] || firstName == nil) &&
    ([lastName isEqualToString:@""] || [lastName isEqualToString:@"(null)"] || lastName == nil))
{
    // do nothing
}
else
{
    aName = [NSString stringWithFormat:@"%@ %@", firstName, lastName];

    if ([firstName isEqualToString:@""] || [firstName isEqualToString:@"(null)"] || firstName == nil)
    {
        aName = [NSString stringWithFormat:@"%@", lastName];
    }

    if ([lastName isEqualToString:@""] || [lastName isEqualToString:@"(null)"] || lastName == nil)
    {
        aName = [NSString stringWithFormat:@"%@", firstName];
    }

    //[self.tableItems addObject:aName];
    NSLog(@"%@ added",aName);
}


        //fetch multiple phone nos. and use only 0th
id person = people[indexPath.row];
ABMultiValueRef multi = ABRecordCopyValue((__bridge ABRecordRef)(person), kABPersonPhoneProperty);
NSString* phone = (__bridge NSString*)ABMultiValueCopyValueAtIndex(multi, 0);
NSLog(@"%@",phone);
[cell.detailTextLabel setText:phone];

[cell.textLabel setText:aName];
return cell;
4

1 回答 1

1

这是一个完整的工作代码

-(void)GetAddressBook
{
    Contacts = [[NSMutableArray alloc]init];

   if (ABAddressBookCreateWithOptions) {

       @try {

            ABAddressBookRef addressBook = ABAddressBookCreate();
            // NSArray *people = (NSArray*)ABAddressBookCopyArrayOfAllPeople(addressBook);
            if (!addressBook) {
                NSLog(@"opening address book");
            }
            CFArrayRef allPeople = ABAddressBookCopyArrayOfAllPeople(addressBook);
            CFIndex nPeople = ABAddressBookGetPersonCount(addressBook);

            NSLog(@"opening address book ==%ld",nPeople);

            for (int i=0;i < nPeople;i++) {

                NSMutableDictionary *dOfPerson=[NSMutableDictionary dictionary];
                ABRecordRef ref = CFArrayGetValueAtIndex(allPeople,i);
                NSString *Contact;
                ABMultiValueRef phones =(__bridge ABMultiValueRef)((__bridge NSString*)ABRecordCopyValue(ref, kABPersonPhoneProperty));
                CFStringRef firstName, lastName;
                NSMutableArray *array = [[NSMutableArray alloc]init];
                NSString *email;
                firstName = ABRecordCopyValue(ref, kABPersonFirstNameProperty);
                lastName  = ABRecordCopyValue(ref, kABPersonLastNameProperty);
                ABMultiValueRef multiValueRef = ABRecordCopyValue(ref, kABPersonEmailProperty);
                array = [(__bridge NSMutableArray *)ABMultiValueCopyArrayOfAllValues(multiValueRef) mutableCopy];
                email = ([array count] > 0) ? array[0] : @"";

                if(firstName)
                {
                    Contact = [NSString stringWithFormat:@"%@", firstName];
                    if(lastName)
                        Contact = [NSString stringWithFormat:@"%@ %@",firstName,lastName];
                }
                [dOfPerson setObject:Contact forKey:@"name"];
                [dOfPerson setObject:[NSString stringWithFormat:@"%d", i] forKey:@"id"];
                [dOfPerson setObject:[NSString stringWithFormat:@"%@",@""] forKey:@"found"];
                [dOfPerson setObject:email forKey:@"email"];

                NSString* mobileLabel;
                for(CFIndex j = 0; j< ABMultiValueGetCount(phones); j++)
                {
                    mobileLabel = (__bridge NSString*)ABMultiValueCopyLabelAtIndex(phones, j);
                    if([mobileLabel isEqualToString:(NSString *)kABPersonPhoneMobileLabel])
                    {
                        [dOfPerson setObject:(__bridge NSString*)ABMultiValueCopyValueAtIndex(phones, j) forKey:@"Phone"];
                    }
                    else if ([mobileLabel isEqualToString:(NSString*)kABPersonPhoneIPhoneLabel])
                    {
                        [dOfPerson setObject:(__bridge NSString*)ABMultiValueCopyValueAtIndex(phones, j) forKey:@"Phone"];
                        break ;
                    }
                }
                [Contacts addObject:dOfPerson];
            }
        }
        @catch (NSException * e) {
            NSLog(@"Exception: %@", e);


        }
        dispatch_async(dispatch_get_main_queue(), ^{



        });

}

电话号码要像 ABMultiValueRef phones =(__bridge ABMultiValueRef)((__bridge NSString*)ABRecordCopyValue(ref, kABPersonPhoneProperty));

于 2013-08-16T10:33:57.907 回答