2012-04-30 26 views
-1

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

现在,我想达到的联系人数据,我想将它们添加到两个不同的阵列,姓名和电话阵列..

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

我有在显示屏幕上的数据问题..我写的东西,但它不能在一个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"]; 
} 

回答

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)]; 

希望这个帮助(可能不包括所有的错误)!

相关问题