2

新しいアプリケーションを 1 つ作成します。アドレス帳から連絡先を追加します。以下のコードを参照してください。ただし、このアドレス帳から連絡先を追加する方法はわかりません。

サンプルコードまたはアイデアを教えてください。

貴重なお時間を割いていただきありがとうございます。

//code for add contact in contact list 

ABRecordRef aRecord = ABPersonCreate(); 
CFErrorRef  anError = NULL; 
ABRecordSetValue(aRecord, kABPersonFirstNameProperty, 
                 txtfirstname.text, &anError); 
ABRecordSetValue(aRecord, kABPersonLastNameProperty, 
                 txtlastName.text, &anError); 
ABRecordSetValue(aRecord, kABPersonBirthdayProperty, 
                 [datepick date], &anError); 
ABRecordSetValue(aRecord, kABPersonPhoneProperty, 
                 txtMobileNo, &anError); 
ABRecordSetValue(aRecord, kABPersonEmailProperty, 
                 txtEmailID, &anError); 
if (anError != NULL) 
{ 
    NSLog(@"error while creating..");
} 
CFStringRef firstName, lastName,birthDay; 
firstName = ABRecordCopyValue(aRecord, kABPersonFirstNameProperty); 
lastName  = ABRecordCopyValue(aRecord, kABPersonLastNameProperty); 
birthDay  = ABRecordCopyValue(aRecord, kABPersonBirthdayProperty); 

ABMutableMultiValueRef email = ABMultiValueCreateMutable(kABMultiStringPropertyType);
ABMultiValueAddValueAndLabel(email, txtEmailID.text, CFSTR("email"), NULL);
ABRecordSetValue(aRecord, kABPersonEmailProperty, email, &anError);
CFRelease(email);

ABMutableMultiValueRef multiPhone =  ABMultiValueCreateMutable(kABMultiStringPropertyType);
ABMultiValueAddValueAndLabel(multiPhone,txtMobileNo.text, kABPersonPhoneMainLabel, NULL);
ABRecordSetValue(aRecord, kABPersonPhoneProperty, multiPhone,nil);
CFRelease(multiPhone);

UIImage *personImage;   
personImage = tempimage;
NSData *dataRef = UIImagePNGRepresentation(personImage); 
CFDataRef dr = CFDataCreate(NULL, [dataRef bytes], [dataRef length]);
CFErrorRef error = NULL; 

ABPersonSetImageData(aRecord, dr, &error);  


ABAddressBookRef addressBook; 
addressBook = ABAddressBookCreate(); 

BOOL isAdded = ABAddressBookAddRecord (addressBook,aRecord,&error);

if(isAdded)
{
    NSLog(@"added..");
}
if (error != NULL) {
    NSLog(@"ABAddressBookAddRecord %@", error);
} 
error = NULL;

BOOL isSaved = ABAddressBookSave (addressBook,&error);

if(isSaved)
{
    UIAlertView* alertView = [[UIAlertView alloc] initWithTitle:@"Contact Save"
                                                        message:nil delegate:self 
                                              cancelButtonTitle:@"Dismiss" otherButtonTitles:nil];
    [alertView show];
    [alertView release];
    NSLog(@"saved..");
}

if (error != NULL) 
{
    NSLog(@"ABAddressBookSave %@", error);
} 

CFRelease(aRecord); 
CFRelease(firstName); 
CFRelease(lastName);
    CFRelease(birthDay); 
CFRelease(addressBook);
4

1 に答える 1

7

Apple のサンプル プロジェクト QuickContacts はこれをカバーしています: http://developer.apple.com/library/ios/#samplecode/QuickContacts/Introduction/Intro.html

お役に立てば幸いです。

于 2012-05-01T10:25:30.400 に答える