时间:2010-07-26 10:40:04

标签: iphone objective-c addressbook

3 个答案:

答案 0 :(得分:15)

答案 1 :(得分:14)

以下是在Iphone地址簿中添加与人物形象联系的代码

ABAddressBookRef addressBook = ABAddressBookCreate();
ABRecordRef person = ABPersonCreate();

//添加人物形象

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

CFErrorRef  anError = NULL;

//全名,公司名称,指定属性

ABRecordSetValue(person,kABPersonFirstNameProperty,full_name,&anError);
ABRecordSetValue(person, kABPersonOrganizationProperty, companyLabel, &anError);
ABRecordSetValue(person, kABPersonJobTitleProperty, designation, &anError);

//多值地址属性

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

答案 2 :(得分:1)

如果有人正在寻找在Swift中添加地址详细信息(街道,城市,州,邮政编码)的方法,那么Apple的网站上没有详细记录。这是我做的方式(只有示例中显示的地址详细信息)...

var addressDictionary: [String:String] = [
    kABPersonAddressStreetKey as String: Contact.address_1,
    kABPersonAddressCityKey as String: Contact.city,
    kABPersonAddressStateKey as String: Contact.state,
    kABPersonAddressZIPKey as String: Contact.zip
]

let multiWork: ABMutableMultiValue = 
  ABMultiValueCreateMutable(
    ABPropertyType(kABMultiDictionaryPropertyType)).takeRetainedValue()
ABMultiValueAddValueAndLabel(multiWork, addressDictionary, kABWorkLabel, nil)
ABRecordSetValue(person, kABPersonAddressProperty, multiWork, nil)