程式碼片段 |
// 特此說明用的main. storyboard@implementation ViewController- (void)viewDidLoad{ [super viewDidLoad];}- (IBAction)add:(id)sender{// 名 NSString *firstName = self.firstnameField.text; // 姓 NSString *lastName = self.lastnameField.text; NSString *homePhone = self.homePhoneField.text;// 有線電話 NSString* mobilePhone = self.mobilePhoneField.text :// 手機NSString* workMail = self.workMailField.text ;NSString* privateMail = self. privateMailField.text ;NSString* country = self.countryField.text ;NSString* state = self. stateField.text ;CFErrorRef error = nil;// 建立ABAddressBook,該函數的第1個參數暫時並未使用,直接傳入NULL即可.ABAddressBookRef ab = ABAddressBookCreateWithOptions(NULL, &error);if(!error){ // 請求訪問使用者地址簿ABAddressBookAccessWithCompletion(ab, ^(bool granted, CFErrorRef error){ // 如果使用者允許訪問地址簿if(granted){// 建立一條新的記錄ABRecordRef rec = ABPersonCreate();// 為rec的kABPersonFirstNameProperty(名字)屬性設定值ABRecordSetValue(rec, kABPersonFirstNameProperty, (__bridge CFStringRef)firstName, NULL);// 為rec的kABPresonLastNameProperty(姓氏)屬性設定值ABRecordSetValue(rec, kABPersonLastNameProperty, (__bridge CFStringRef)lastName, NULL);// 建立ABMutableMultiValueRef來管理多個電話號碼ABMutableMultiValueRef phoneValue = ABMultiValueCreateMutable(kABPersonPhoneProperty);// 為家庭的電話號碼添加labelABMultiValueAddValueAndLabel(phoneValue , (__bridge CFTypeRef)homePhone), kABHomeLabel, NULL);// 為移動的電話號碼添加labelABMultiValueAddValueAndLabel(phoneValue , (__bridge CFTypeRef)mobilePhone), kABPersonPhoneMobileLabel, NULL); // 為rec的kABPersonPhoneProperty(電話)屬性設定值ABRecordSetValue(rec, kABPersonPhoneProperty, phoneValue, NULL);// 建立ABMutableMultiValueRef來管理多個電子郵件ABMutableMultiValueRef mailValue = ABMultiValueCreateMutable(kABPersonEmailProperty);// 為工作的電子郵件添加labelABMultiValueAddValueAndLabel(mailValue , (__bridge CFStringRef)workMail, (__bridge CFStringRef)@”工作”, NULL);// 為私人的電子郵件添加labelABMultiValueAddValueAndLabel(mailValue , (__bridge CFStringRef)privateMail, (__bridge CFStringRef)@”私人”, NULL);// 為rec的kABPersonEmailProperty(電子郵件)屬性設定值ABRecordSetValue(rec, kABPersonMailProperty, mailValue, NULL);// 建立ABMutableMultiValueRef來管理多個地址ABMutableMultiValueRef addrValue = ABMultiValueCreateMutable(kABPersonAddressProperty);// 建立一條新的ABRecordRef記錄,這條記錄不包含任何屬性NSDictionary *addrDict = [NSDictionary dictionaryWithObjectsAndKeys:country, kABPersonAddressCountryKey, state, kABPersonAddressStateKey, nil];// 為住址的地址添加labelABMultiValueAddValueAndLabel(addrValue, (__bridge CFTypeRef)addrDict, (__bridge CFStringRef)@”地址”, NULL);// 為rec的kABPersonAddressProperty(地址)屬性設定值ABRecordSetValue(rec, kABPersonAddressProperty, addrValue, NULL);BOOL result = ABAddressBookAddRecord(ab, rec, NULL); if(result) { // 將程式所做的修改儲存到地址簿中,如果儲存成功 if(ABAddressBookSave(ab, NULL)) { [self showAlert:@”成功添加新的連絡人”]; } else { [self showAlert:@”添加失敗”]; } } } })}- (IBAction)finishEdit:(id)sender{ [sender resignFirstResponder];}- (void)showAlert:(NSString *)msg{ // 使用UIAlertView顯示msg資訊 [ [ [UIAlertView alloc] initWithTitle:@” 提 示 ” message:msg delegate:nil cancelButtonTitle:@”確定” otherButtonTitles:nil] show ];}@end// 注釋:因為iOS系統要求地址必須是{國家:國家值, 省份:省份值, 城市:城市值}這種NSDictionary對象,所以建立了一個NSDictionary對象作為地址值 |