首先,需要导入AddressBook.framework框架,然后在头文件中加入:#import <AddressBook/AddressBook.h>
Ios代码
CFErrorRef error = NULL; ABAddressBookRef addressBook = ABAddressBookCreate(); ABRecordRef newPerson= ABPersonCreate(); ABRecordSetValue(newPerson, kABPersonFirstNameProperty, @"Eric", &error); ABRecordSetValue(newPerson, kABPersonLastNameProperty, @"Gao", &error); ABRecordSetValue(newPerson, kABPersonJobTitleProperty, @"市场部经理", &error); ABRecordSetValue(newPerson, kABPersonOrganizationProperty, @"移动技术研发", &error); ABMutableMultiValueRef multiPhone = ABMultiValueCreateMutable(kABMultiStringPropertyType); ABMultiValueAddValueAndLabel(multiPhone, @"12345678", kABPersonPhoneMainLabel, NULL); ABRecordSetValue(newPerson, kABPersonPhoneProperty, multiPhone, &error); CFRelease(multiPhone); ABMutableMultiValueRef multiEmail = ABMultiValueCreateMutable(kABMultiStringPropertyType); ABMultiValueAddValueAndLabel(multiEmail, @"别墅", kABHomeLabel, NULL); ABRecordSetValue(newPerson, kABPersonEmailProperty, multiEmail, &error); CFRelease(multiEmail); ABMutableMultiValueRef urlMultiValue = ABMultiValueCreateMutable(kABMultiStringPropertyType); ABMultiValueAddValueAndLabel(urlMultiValue,@"www.google.cn", kABPersonHomePageLabel, NULL); ABRecordSetValue(newPerson, kABPersonURLProperty, urlMultiValue, &error); CFRelease(urlMultiValue); ABMutableMultiValueRef multiAddress = ABMultiValueCreateMutable(kABMultiDictionaryPropertyType); NSMutableDictionary *addressDictionary = [[NSMutableDictionary alloc] init]; [addressDictionary setObject:@"北京市海淀区中关村" forKey:(NSString *) kABPersonAddressStreetKey]; ABMultiValueAddValueAndLabel(multiAddress, addressDictionary, kABWorkLabel, NULL); ABRecordSetValue(newPerson, kABPersonAddressProperty, multiAddress, &error); CFRelease(multiAddress); UIImage *image = [UIImage imageNamed:@"head.png"]; if (image) { NSData *dataRef = UIImagePNGRepresentation(image); CFDataRef dr = CFDataCreate(NULL, [dataRef bytes], [dataRef length]); ABPersonSetImageData(newPerson, dr, &error); CFRelease(dr); } ABAddressBookAddRecord(addressBook, newPerson, &error); CFRelease(newPerson); ABAddressBookSave(addressBook, &error);
效果图: