修改聯系人先從底層地址簿中加載一條ABRecordRef記錄,然后對這條ABRecordRef記錄的屬性值進行修改,修改完成后把這條修改后的ABRecordRef記錄存入地址簿即可.
修改聯系人的步驟大致如下 | |
1 | 獲取底層地址簿中已有的聯系人對應的ABRecordRef記錄 |
2 | 根據需要修改的屬性,調用ABRecordSetValue()函數修改ABRecordRef記錄中指定屬性的值 |
3 | 修改完成后調用ABAddressBookSave()函數將剛剛所做的修改保存到底層地址簿 |
代 碼 片 段 | #import <AddressBook/AddressBook.h>#import “LCUpdatePersonViewController.h”#define PHONE_PROPERTY_KEY @”phone”#define MAIL_PROPERTY_KEY @”mail”@interface LCUpdatePersonViewController(){ ABAddressBookRef ab; // 定義ABRecordRef 類型的變量保存當前正在更新的記錄 ABRecordRef rec; // 使用該變量定義當前動態添加的行的Y坐標 NSInteger curLineY; // 定義一個NSMUtableDictionary來保存所有動態添加的文本框 NSMUtableDictionary *textFields; // 定義ABMutableMultiValueRef變量記錄正在修改的電話號碼屬性值 ABMutableMultiValueRef phoneValue; // 定義ABMutableMultiValueRef變量記錄正在修改的電子郵件屬性值 ABMutableMultiValueRef mailValue;}@end@implementation LCUpdatePersonViewController- (void)viewDidLoad{ [super viewDidLoad];textFields = [NSMutableDictionary dictionary];curLineY = 120;CFErrorRef error = nil;// 創建ABAddressBook, 該函數的第1個參數暫時并未使用,直接傳入NULL即可ab = ABAddressBookRequestaccessWithCompletion(ab,^(bool granted, CFErrorRef error){ // 如果用戶允許訪問地址簿 if(granted) { // 獲取ID為1的ABRecordRef記錄 rec = ABAddressBookGetPersonWithRecordID(ab, 1); // 獲取rec記錄中kABPersonFirstNameProperty(名字)屬性的值 NSString *firstName = (__bridge NSString *)ABRecordCopyValue(rec, kABPersonFirstNameProperty); // 獲取rec記錄中kABPersonLastNameProperty(姓氏)屬性值 NSString *lastName = (__bridge NSString*)ABRecordCopyValue(rec, kABPersonLastNameProperty ); // 控制界面上文本框顯示rec記錄中姓氏、名字的屬性值 self.firstNameField.text = firstName; self.lastNameField.text = lastName; // 獲取rec記錄中kABPersonPhoneProperty(電話號碼)屬性值 phoneValue = ABRecordCopyValue(rec, kABPersonPhoneProperty); // 調用addLabelAndTextField:propertyKey:方法顯示電話號碼[self addLabelAndTextField:phoneValue propertyKey:PHONE_PROPERTY_KEY];// 獲取rec記錄中kABPersonEmailProperty(電子郵件)屬性值mailValue = ABRecordCopyValue(rec, kABPersonEmailProperty);// 調用addLabelAndTextField:propertyKey:方法顯示電子郵件[self addLabelAndTextField:mailValue propertyKey:MAIL_PROPERTY_KEY];}});}}- (void) addLabelAndTextField:(ABMutableMultiValueRef) multiValue propertyKey:(NSString *)property{ // 獲取multiValue包含的數據條數 NSInteger num = ABMultiValueGetCount(multiValue); NSMutableArray *textFieldArray = [NSMutableArray array]; // 依次遍歷multiValue所包含的每條數據 for (int i = 0; i < num; i++ ){ curLineY += 38; // 創建UILabel,顯示本條數據的label UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(20, curLineY, 70, 30)];NSString *labelStr = (__bridge NSString*)ABAddressBookCopyLocalizedLabel(ABMultiValueCopyLabelAtIndex(multiValue, i));label.text = labelStr;// 將該UILabel添加到程序界面上[self.view performSelectorOnMainThread:@selector(addSubview:)withObject:label waitUntilDone:YES]; // 創建UITextField顯示本條數據的value UITextField *textField = [[UITextField alloc] initWithFrame:CGRectMake(98, curLineY, 202, 30)]; textField.borderStyle = UITextBorderStyleRoundedRect; NSString *valueStr = (__bridge NSString*)ABMultiValueCopyValueAtIndex(multiValue, i); textField.text = valueStr; [textField addTarget:self action:@selector(finishEdit:) forControlEvents:UIControlEventEditingDidEndOnExit];// 使用NSArray集合來保存動態創建的UITextField控件 [textFieldArray addObject: textField]; // 將UITextField添加到程序界面上 [self.view performSelectorOnMainThread:@selector(addSubview:) withObject:textField waitUntilDone:YES];} // 將程序動態生成的所有textField保存到NSMutableDictionary中 [textFields setValue:textFieldArray forKey:property];}- (void) updateMultiValue: (ABMutableMultiValueRef) multiValue propertyKey :( NSString* )propertyKey property:(ABPropertyID) property{ // 取出該屬性對應的所有UITextView組成的NSArray NSArray *textFieldArray = textFields[propertyKey]; NSInteger num = textFieldArray.count; // 創建一個新的ABMutableMultiValueRef ABMutableMultiValueRef newMulti = ABMultiValueCreateMutable(property); // 遍歷UITextView組成的NSArray集合中的每個UITextField控件 for(int i = 0; i< num; i++) { // 獲取第i個UITextField控件中的字符串,該字符串作為新的值 NSString *value = ((UITextField*)textFieldArray[i]).text; // 獲取第i 條數據原有的label CFStringRef label = ABMultiValueCopyLabelAtIndex(multiValue, 0); // 添加新的值和原有的label(label不需要修改) ABMultiValueAddValueAndLabel(newMutli, (__bridgeCFStringRef)value, label, NULL);}ABRecordSetValue(rec, property, newMutli, NULL);}- (IBAction)update:(id)sender{NSString *firstName = self.firstnameField.text;NSString *lastName = self.lastnameField.text;// 修改rec記錄中的kABPersonFirstNameProperty、kABPersonLastNameProperty屬性ABRecordSetValue(rec, kABPersonFirstNameProperty, (__bridge CFStringRef)firstName, NULL);ABRecordSetValue(rec, kABPersonLastNameProperty, (__bridge CFStringRef)lastName, NULL);// 調用updateMultiValue:propertyKey:property:方法修改ABRecordRef的// kABPersonPhoneProperty屬性[self updateMultiValue:phoneValue propertyKey:PHONE_PROPERTY_KEY property:kABPersonPhoneProperty];// 調用updateMultiValue:propertyKey:property:方法修改ABRecordRef的// kABPersonEmailProperty屬性[self updateMultiValue:mailValue propertyKey:MAIL_PROPERTY_KEY property:kABPersonEmailProperty]; 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
|
注意 | 為了讓界面能動態顯示ABMutableMultiValueRef包含的每一條數據,程序定義了addLabelAndTextField:propertyKey:方法來動態加載ABMutableMultiValueRef中的每一條數據----程序為每條數據添加一行,用UILabel顯示這條數據的label,用UITextField顯示這條數據的value
|
效果圖:
新聞熱點
疑難解答