-1

大家..我正在尝试为iPhone开发一个应用程序,它基本上处理ABAddressBook和联系人数据......我的目标是通过电子邮件将所有联系人数据(第一步,姓名和电话)发送到一个文件中......

现在,我正在尝试访问联系人数据,并且我想将它们添加到两个不同的数组中,名称和电话数组..

起初,当我按下“列出联系人”按钮时,我试图查看屏幕中的所有数据。数据应显示在屏幕上。然后当我按下第二个按钮“发送联系人”时,它应该将文件发送到电子邮件帐户。这就是我的应用程序的工作方式。

我在屏幕上显示数据时遇到问题。我写了一些东西,但在 textView 的屏幕上没有给出任何内容。你能帮我解决这个问题吗?

这是列出联系人的代码(listCon 方法):


-(IBAction)listCon:(id)sender
{

    NSMutableArray *names = [[NSMutableArray alloc] init];

    NSMutableArray *numbers1= [[NSMutableArray array] init];

    NSMutableArray *numbers2= [[NSMutableArray array] init];

    NSMutableArray *numbers3= [[NSMutableArray array] init];

    ABAddressBookRef addressBook = ABAddressBookCreate();

    if (addressBook != nil)
    {

        NSLog(@"Successfully accessed the address book.");

        CFArrayRef allPeople = ABAddressBookCopyArrayOfAllPeople(addressBook);
        CFIndex nPeople= ABAddressBookGetPersonCount(addressBook);

        NSUInteger peopleCounter = 0;
        for (peopleCounter = 0;peopleCounter < nPeople; peopleCounter++)
        {

            ABRecordRef thisPerson = CFArrayGetValueAtIndex(allPeople,peopleCounter);

            NSString *contactFirstLast = [NSString stringWithFormat:@"%,%",ABRecordCopyValue(thisPerson, kABPersonFirstNameProperty), ABRecordCopyValue(thisPerson,kABPersonLastNameProperty)];

            [names insertObject:contactFirstLast atIndex:peopleCounter];

            ABMultiValueRef phoneNumbers = ABRecordCopyValue(thisPerson,kABPersonPhoneProperty);

            NSString *firstPhone = (__bridge_transfer NSString*) ABMultiValueCopyValueAtIndex(phoneNumbers, 0);

            NSString *secondPhone = (__bridge_transfer NSString*) ABMultiValueCopyValueAtIndex(phoneNumbers, 1);

            NSString *thirdPhone = (__bridge_transfer NSString*) ABMultiValueCopyValueAtIndex(phoneNumbers, 2);

            [numbers1 insertObject:firstPhone atIndex:peopleCounter];
            [numbers2 insertObject:secondPhone atIndex:peopleCounter];                             
            [numbers3 insertObject:thirdPhone atIndex:peopleCounter];
    }          
}

myView.text=[names componentsJoinedByString:@"\n\n"];     

myView.text=[numbers1 componentsJoinedByString:@"\n\n"];    

myView.text=[numbers2 componentsJoinedByString:@"\n\n"];

myView.text=[numbers3 componentsJoinedByString:@"\n\n"];
}   
4

1 回答 1

0

只是看一眼你的代码,你不能这样做:

NSString *contactFirstLast = [NSString stringWithFormat:@"%,%",ABRecordCopyValue(thisPerson, kABPersonFirstNameProperty), ABRecordCopyValue(thisPerson,kABPersonLastNameProperty)];

有几个错误:首先%在你stringWithFormat:的不是格式说明符;你可能在想%@。其次,复制 的值kABPersonFirstNameProperty将返回 a CFStringRef,这不是您想要在文本字段中显示名称的内容。您必须免费桥接ABRecordCopyValue(). 您可以通过在您的 's(__bridge_transfer NSString *)前面添加这一行来做到这一点 - - ABRecordCopyValue()进行所有更正后,它应该如下所示:

NSString *contactFirstLast = [NSString stringWithFormat:@"%@,%@", (__bridge_transfer NSString *)ABRecordCopyValue(thisPerson, kABPersonFirstNameProperty), (__bridge_transfer NSString *)ABRecordCopyValue(thisPerson,kABPersonLastNameProperty)];

希望有帮助(可能无法涵盖所有​​错误)!

于 2012-07-02T20:18:20.437 回答