首页 > 代码库 > iOS.访问通讯录.02.写入联系人
iOS.访问通讯录.02.写入联系人
一、创建联系人
1、创建联系人记录
ABRecord person = ABPersonCreate();
2、保存或修改单值属性
bool ABRecordSetValue( ABRecordRef record, ABPropertyID property, CFTypeRef value, CFErrorRef *error);
例子:
CFError error = NULL;// 保存姓名ABRecordSetValue(person,kABPersonFirstNameProperty,(__bridge CFTypeRef)self.txtFirstName.text,&error);ABRecordSetValue(person,kABPersonLastNameProperty,(__bridge CFTypeRef)self.txtLastName.text,&error);
3、保存或修改多值属性
bool ABMultiValueAddValueAndLabel( ABMutableMultiValueRef multiValue, CFTypeRef value, CFStringRef label, ABMultiValueIdentifier *outIdentifier);
例:
// 创建可变多值类型对象ABMutableMultiValueRef multi = ABMultiValueCreateMutable(kABMultiStringProper);// 设置电话号码ABMultiValueAddValueAndLabel(multi,(__bridge CFTypeRef)self.txtMobile.text,kABPersonPhoneMobileLabel,NULL);ABMultiValueAddValueAndLabel(multi,(__bridge CFTypeRef)self.txtIPhone.text,kABPersonPhoneIPhoneLabel,NULL);// 添加电话号码到记录ABRecordSetValue(person,kABPersonPhoneProperty,multi,&error);CFRelease(multi);
4、添加记录到数据库
bool ABAddressBookAddRecord( ABAddressBookRef addressBook, ABRecordRef record, CFErrorRef *error);bool ABAddressBookSave( ABAddressBookSave addressBook, CFErrorRef *error);
例:
ABAddressBookRef addressBook = ABAddressBookCreateWithOptions(NULL,&error);ABAddressBookAddRecord(addressBook,person,&error);ABAddressBookSave(addressBook,&error);CFRelease(person);CFRelease(addressBook);
二、修改联系人信息
1、获取联系人记录
ABRecordRef ABAddressBookGetPersonWithRecordID( ABAddressBookRef addressBook, ABRecordID recordID);
三、删除联系人信息
bool ABAddressBookRemoveRecord( ABAddressBookRef addressBook, ABRecordRef record, CFErrorRef *error);
四、实战代码
#import <UIKit/UIKit.h>#import <AddressBook/AddressBook.h>#import "T20140622161906DetailViewController.h"@interface T20140622161906ViewController : UITableViewController@property (strong,nonatomic) NSArray *listContacts;@end
#import "T20140622161906ViewController.h"@interface T20140622161906ViewController ()@end@implementation T20140622161906ViewController- (void)viewDidLoad{ [super viewDidLoad]; CFErrorRef error = NULL; ABAddressBookRef addressBook = ABAddressBookCreateWithOptions(NULL, &error); ABAddressBookRequestAccessWithCompletion(addressBook, ^(bool granted, CFErrorRef error) { if (granted) { //查询所有 self.listContacts = CFBridgingRelease(ABAddressBookCopyArrayOfAllPeople(addressBook)); [self.tableView reloadData]; CFRelease(addressBook); } });}// 删除联系人后 重新加载列表信息- (void)viewWillAppear:(BOOL)animated{ [super viewWillAppear:YES]; CFErrorRef error = NULL; ABAddressBookRef addressBook = ABAddressBookCreateWithOptions(NULL, &error); self.listContacts = CFBridgingRelease(ABAddressBookCopyArrayOfAllPeople(addressBook)); CFRelease(addressBook); [self.tableView reloadData]; }- (void)didReceiveMemoryWarning{ [super didReceiveMemoryWarning]; // Dispose of any resources that can be recreated.}#pragma mark - Table view data source- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{ return 1;}- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{ return [self.listContacts count];}- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{ static NSString *CellIdentifier = @"reUseCell"; UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; if (cell == nil) { cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier]; } ABRecordRef thisPerson = CFBridgingRetain([self.listContacts objectAtIndex:[indexPath row]]); NSString *firstName = CFBridgingRelease(ABRecordCopyValue(thisPerson, kABPersonFirstNameProperty)); firstName = firstName != nil?firstName:@""; NSString *lastName = CFBridgingRelease(ABRecordCopyValue(thisPerson, kABPersonLastNameProperty)); lastName = lastName != nil?lastName:@""; cell.textLabel.text = [NSString stringWithFormat:@"%@ %@",firstName,lastName]; CFRelease(thisPerson); return cell;}- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender{ if ([[segue identifier] isEqualToString:@"showDetail"]) { NSIndexPath *indexPath = [self.tableView indexPathForSelectedRow]; ABRecordRef thisPerson = CFBridgingRetain([self.listContacts objectAtIndex:[indexPath row]]); T20140622161906DetailViewController *detailViewController = [segue destinationViewController]; ABRecordID personID = ABRecordGetRecordID(thisPerson); NSNumber *personIDAsNumber = [NSNumber numberWithInt:personID]; detailViewController.personIDAsNumber = personIDAsNumber; CFRelease(thisPerson); }}@end
#import <UIKit/UIKit.h>#import <AddressBook/AddressBook.h>@interface T20140622161906DetailViewController : UITableViewController@property (weak, nonatomic) IBOutlet UIImageView *imageView;@property (weak, nonatomic) IBOutlet UILabel *lblName;@property (weak, nonatomic) IBOutlet UITextField *txtMobile;@property (weak, nonatomic) IBOutlet UITextField *txtIPhone;@property (weak, nonatomic) IBOutlet UITextField *txtWorkEmail;@property (weak, nonatomic) IBOutlet UITextField *txtHomeEmail;@property (strong, nonatomic) NSNumber* personIDAsNumber;- (IBAction)saveClick:(id)sender;- (IBAction)deleteClick:(id)sender;@end
#import "T20140622161906DetailViewController.h"@interface T20140622161906DetailViewController ()@end@implementation T20140622161906DetailViewController- (id)initWithStyle:(UITableViewStyle)style{ self = [super initWithStyle:style]; if (self) { // Custom initialization } return self;}- (void)viewDidLoad{ [super viewDidLoad]; ABRecordID personID = [self.personIDAsNumber intValue]; CFErrorRef error = NULL; ABAddressBookRef addressBook = ABAddressBookCreateWithOptions(NULL, &error); ABRecordRef person = ABAddressBookGetPersonWithRecordID(addressBook, personID); //取得姓名属性 NSString *firstName = CFBridgingRelease(ABRecordCopyValue(person, kABPersonFirstNameProperty)); firstName = firstName != nil?firstName:@""; NSString *lastName = CFBridgingRelease(ABRecordCopyValue(person, kABPersonLastNameProperty)); lastName = lastName != nil?lastName:@""; [self.lblName setText: [NSString stringWithFormat:@"%@ %@",firstName,lastName]]; //取得Email属性 ABMultiValueRef emailsProperty = ABRecordCopyValue(person, kABPersonEmailProperty); NSArray* emailsArray = CFBridgingRelease(ABMultiValueCopyArrayOfAllValues(emailsProperty)); for(int index = 0; index< [emailsArray count]; index++){ NSString *email = [emailsArray objectAtIndex:index]; NSString *emailLabel = CFBridgingRelease(ABMultiValueCopyLabelAtIndex(emailsProperty, index)); if ([emailLabel isEqualToString:(NSString*)kABWorkLabel]) { [self.txtWorkEmail setText:email]; } else if ([emailLabel isEqualToString:(NSString*)kABHomeLabel]) { [self.txtHomeEmail setText:email]; } else { NSLog(@"%@: %@", @"其它Email", email); } } CFRelease(emailsProperty); //取得电话号码属性 ABMultiValueRef phoneNumberProperty = ABRecordCopyValue(person, kABPersonPhoneProperty); NSArray* phoneNumberArray = CFBridgingRelease(ABMultiValueCopyArrayOfAllValues(phoneNumberProperty)); for(int index = 0; index< [phoneNumberArray count]; index++){ NSString *phoneNumber = [phoneNumberArray objectAtIndex:index]; NSString *phoneNumberLabel = CFBridgingRelease(ABMultiValueCopyLabelAtIndex(phoneNumberProperty, index)); if ([phoneNumberLabel isEqualToString:(NSString*)kABPersonPhoneMobileLabel]) { [self.txtMobile setText:phoneNumber]; } else if ([phoneNumberLabel isEqualToString:(NSString*)kABPersonPhoneIPhoneLabel]) { [self.txtIPhone setText:phoneNumber]; } else { NSLog(@"%@: %@", @"其它电话", phoneNumber); } } CFRelease(phoneNumberProperty); //取得个人图片 if (ABPersonHasImageData(person)) { NSData *photoData =http://www.mamicode.com/ CFBridgingRelease(ABPersonCopyImageData(person)); if(photoData){ [self.imageView setImage:[UIImage imageWithData:photoData]]; } } CFRelease(addressBook); }- (void)didReceiveMemoryWarning{ [super didReceiveMemoryWarning]; }- (IBAction)saveClick:(id)sender { ABRecordID personID = [self.personIDAsNumber intValue]; CFErrorRef error = NULL; ABAddressBookRef addressBook = ABAddressBookCreateWithOptions(NULL, &error); ABRecordRef person = ABAddressBookGetPersonWithRecordID(addressBook, personID); // 设置电话号码 ABMutableMultiValueRef multi = ABMultiValueCreateMutable(kABMultiStringPropertyType); ABMultiValueAddValueAndLabel(multi, (__bridge CFTypeRef)self.txtMobile.text, kABPersonPhoneMobileLabel, NULL); ABMultiValueAddValueAndLabel(multi, (__bridge CFTypeRef)self.txtIPhone.text, kABPersonPhoneIPhoneLabel, NULL); // 添加电话号码到数据库 ABRecordSetValue(person, kABPersonPhoneProperty, multi, &error); CFRelease(multi); // 设置Email属性 multi = ABMultiValueCreateMutable(kABMultiStringPropertyType); ABMultiValueAddValueAndLabel(multi, (__bridge CFTypeRef)self.txtHomeEmail.text, kABHomeLabel, NULL); ABMultiValueAddValueAndLabel(multi, (__bridge CFTypeRef)self.txtWorkEmail.text, kABWorkLabel, NULL); // 添加Email到数据库 ABRecordSetValue(person, kABPersonEmailProperty, multi, &error); CFRelease(multi); // 保存到数据库 ABAddressBookSave(addressBook, &error); CFRelease(addressBook); //导航回根视图控制器ViewController [self.navigationController popToRootViewControllerAnimated:YES];}- (IBAction)deleteClick:(id)sender { ABRecordID personID = [self.personIDAsNumber intValue]; CFErrorRef error = NULL; ABAddressBookRef addressBook = ABAddressBookCreateWithOptions(NULL, &error); ABRecordRef person = ABAddressBookGetPersonWithRecordID(addressBook, personID); //删除记录 ABAddressBookRemoveRecord(addressBook,person,&error); // 保存到数据库 ABAddressBookSave(addressBook, &error); CFRelease(addressBook); //导航回根视图控制器ViewController [self.navigationController popToRootViewControllerAnimated:YES]; }@end
#import <UIKit/UIKit.h>#import <AddressBook/AddressBook.h>@interface T20140622161906AddViewController : UITableViewController@property (weak, nonatomic) IBOutlet UITextField *txtLastName;@property (weak, nonatomic) IBOutlet UITextField *txtFirstName;@property (weak, nonatomic) IBOutlet UITextField *txtMobile;@property (weak, nonatomic) IBOutlet UITextField *txtIPhone;@property (weak, nonatomic) IBOutlet UITextField *txtWorkEmail;@property (weak, nonatomic) IBOutlet UITextField *txtHomeEmail;- (IBAction)saveClick:(id)sender;- (IBAction)cancelClick:(id)sender;@end
#import "T20140622161906AddViewController.h"@interface T20140622161906AddViewController ()@end@implementation T20140622161906AddViewController- (id)initWithStyle:(UITableViewStyle)style{ self = [super initWithStyle:style]; if (self) { // Custom initialization } return self;}- (void)viewDidLoad{ [super viewDidLoad]; // Uncomment the following line to preserve selection between presentations. // self.clearsSelectionOnViewWillAppear = NO; // Uncomment the following line to display an Edit button in the navigation bar for this view controller. // self.navigationItem.rightBarButtonItem = self.editButtonItem;}- (void)didReceiveMemoryWarning{ [super didReceiveMemoryWarning]; // Dispose of any resources that can be recreated.}- (IBAction)saveClick:(id)sender { CFErrorRef error = NULL; ABAddressBookRef addressBook = ABAddressBookCreateWithOptions(NULL, &error); ABRecordRef person = ABPersonCreate(); //保存姓名 ABRecordSetValue(person, kABPersonFirstNameProperty, (__bridge CFTypeRef)self.txtFirstName.text, &error); ABRecordSetValue(person, kABPersonLastNameProperty, (__bridge CFTypeRef)self.txtLastName.text, &error); // 设置电话号码 ABMutableMultiValueRef multi = ABMultiValueCreateMutable(kABMultiStringPropertyType); ABMultiValueAddValueAndLabel(multi, (__bridge CFTypeRef)self.txtMobile.text, kABPersonPhoneMobileLabel, NULL); ABMultiValueAddValueAndLabel(multi, (__bridge CFTypeRef)self.txtIPhone.text, kABPersonPhoneIPhoneLabel, NULL); // 添加电话号码到记录 ABRecordSetValue(person, kABPersonPhoneProperty, multi, &error); CFRelease(multi); // 设置Email属性 multi = ABMultiValueCreateMutable(kABMultiStringPropertyType); ABMultiValueAddValueAndLabel(multi, (__bridge CFTypeRef)self.txtHomeEmail.text, kABHomeLabel, NULL); ABMultiValueAddValueAndLabel(multi, (__bridge CFTypeRef)self.txtWorkEmail.text, kABWorkLabel, NULL); // 添加Email到记录 ABRecordSetValue(person, kABPersonEmailProperty, multi, &error); CFRelease(multi); // 增加记录到数据库 ABAddressBookAddRecord(addressBook, person, &error); // 保存到数据库 ABAddressBookSave(addressBook, &error); CFRelease(person); CFRelease(addressBook); [self dismissViewControllerAnimated:YES completion:nil]; }- (IBAction)cancelClick:(id)sender { [self dismissViewControllerAnimated:YES completion:nil];}@end
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。