如何将联系人信息添加到应用程序中的地址簿中

时间:2013-05-24 04:59:53

标签: iphone objective-c xcode ipad addressbookui

我想在我的应用程序中将联系人添加到我的地址簿中。 就像我有一个名称和电话号码的文本字段,一旦我点击它按钮它应该添加到我的地址簿。

我在stackoverflow How to add a contact from my app to the iOS address book中经历了很多问题,但我不明白,所以任何人都可以通过示例代码告诉我如何实现这一点,或者如果你有任何关于此的教程链接,那就太棒了。

1 个答案:

答案 0 :(得分:0)

如果你想添加一个有图像的人的地址,这将有助于你先生 首先,您必须制作ABAddressBookRef对象。

ABAddressBookRef addressBook = ABAddressBookCreate();
    ABRecordRef person = ABPersonCreate();
    // Add Person Image

    NSData *dataRef = UIImagePNGRepresentation(personImageView.image);
    ABPersonSetImageData(person, (CFDataRef)dataRef, nil);

    CFErrorRef  anError = NULL;
    // Full Name, Company Name, Designation Property

    ABRecordSetValue(person,kABPersonFirstNameProperty,full_name,&anError);
    ABRecordSetValue(person, kABPersonOrganizationProperty, companyLabel, &anError);
    ABRecordSetValue(person, kABPersonJobTitleProperty, designation, &anError);
    // Multi value Address Property

    ABMutableMultiValueRef multiAddress = ABMultiValueCreateMutable(kABMultiDictionaryPropertyType);'
    NSMutableDictionary *addressDictionary = [[NSMutableDictionary alloc] init];

    [addressDictionary setObject:@"Chicago" forKey:(NSString *)kABPersonAddressCityKey];
    [addressDictionary setObject:@"IL" forKey:(NSString *)kABPersonAddressStateKey];
    [addressDictionary setObject:@"60654" forKey:(NSString *)kABPersonAddressZIPKey];

    ABMultiValueAddValueAndLabel(multiAddress, addressDictionary, (CFStringRef)@"Address", NULL);
    ABRecordSetValue(person, kABPersonAddressProperty, multiAddress,&anError);
    CFRelease(multiAddress);

    ABMutableMultiValueRef websiteMultiValue = ABMultiValueCreateMutable(kABPersonURLProperty);
    ABMultiValueAddValueAndLabel(websiteMultiValue, web_URL, (CFStringRef)@"Website", NULL);
    ABRecordSetValue(person, kABPersonURLProperty, websiteMultiValue, &anError);


    ABMutableMultiValueRef emailMultiValue = ABMultiValueCreateMutable(kABPersonEmailProperty);
    ABMultiValueAddValueAndLabel(emailMultiValue, email, (CFStringRef)@"Email", NULL);
    ABRecordSetValue(person, kABPersonEmailProperty, emailMultiValue, &anError);

    ABAddressBookAddRecord(addressBook, person, &anError);

希望这有助于先生..