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);
}
私は今ここに2つの問題があります:
1) NSLog では、ツイートにはすべての値が含まれていますが、次のような形式になっています: FirstName LastName ---> ABMultiValueRef 0x6c26760 with 1 value(s) 0: $!!$ (0x6c26b60) - PhoneNumber (0x6c26b80)
この形式で必要です FirstName LastName ---> PhoneNumber
2) Contact.txt ファイルが読み込まれません。NSLog のパスは正しいです。ここで何が悪いのかアドバイスしてください。