我使用 Apple 的地址簿和以下代表 ABNewPersonViewControllerDelegate、ABPeoplePickerNavigationControllerDelegate 和 ABPersonViewControllerDelegate 创建了一个简单的联系人应用程序,它具有以下功能:创建、编辑和删除联系人。现在我想添加一个选项,通过该选项,我创建的所有联系人以及电话号码都将导入文本文件。这是我的代码:
-(void)fetchContacts
{
addressBooks =ABAddressBookCreate();
CFArrayRef people = ABAddressBookCopyArrayOfAllPeople(addressBooks);
for (CFIndex i = 0; i < CFArrayGetCount(people); i++)
{
person12 = CFArrayGetValueAtIndex(people, i);
NSString *tweet=[[NSString stringWithFormat:@"%@",(__bridge_transfer NSString *)ABRecordCopyValue(person12, kABPersonFirstNameProperty)] stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceCharacterSet]];
if(CFBridgingRelease(ABRecordCopyValue(person12,kABPersonLastNameProperty))!=NULL)
{
tweet=[tweet stringByAppendingString:@" "];
tweet=[tweet stringByAppendingString:[[NSString stringWithFormat:@"%@",(__bridge_transfer NSString *)ABRecordCopyValue(person12, kABPersonLastNameProperty)] stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceCharacterSet]]];
}
if(CFBridgingRelease(ABRecordCopyValue(person12,kABPersonFirstNameProperty))!=NULL)
{
tweet=[tweet stringByAppendingString:@" ---> "];
tweet=[tweet stringByAppendingString:[[NSString stringWithFormat:@"%@",(__bridge_transfer NSString *)ABRecordCopyValue(person12, kABPersonPhoneProperty)] stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceCharacterSet]]];
}
NSLog(@"%@",tweet);
NSString* documentPlistPath = [[NSBundle mainBundle] pathForResource:@"Contacts" ofType:@"txt"];
[tweet writeToFile:documentPlistPath atomically:NO encoding:NSUTF8StringEncoding error:nil];
NSLog(@"%@",documentPlistPath);
}
CFBridgingRelease(people);
}
我现在有两个问题:
1) 在 NSLog 上,推文包含所有值,但格式如下:FirstName LastName ---> ABMultiValueRef 0x6c26760 with 1 value(s) 0: $!!$ (0x6c26b60) - PhoneNumber (0x6c26b80)
我需要这种格式 FirstName LastName ---> PhoneNumber
2) Contact.txt 文件没有被填充。在 NSLog 路径是正确的。请指教这里有什么问题。