联系xcode中的数据

时间:2012-04-30 09:06:46

标签: xcode contacts abaddressbook abrecordref

大家.. 我正在尝试为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"];
}   

1 个答案:

答案 0 :(得分:0)

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

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

有几个错误:%中的stringWithFormat:首先不是格式说明符;你可能在考虑%@。其次,复制kABPersonFirstNameProperty的值将返回CFStringRef,而这不是您要在文本字段中显示名称的内容。您必须免费桥接ABRecordCopyValue()的结果。您可以在(__bridge_transfer NSString *)之前添加此行 - ABRecordCopyValue() - 来执行此操作。通过所有更正,它应该如下所示:

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

希望这个帮助(可能不会涵盖所有错误)!