iOS.访问通讯录.02.写入联系人
生活随笔
收集整理的這篇文章主要介紹了
iOS.访问通讯录.02.写入联系人
小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.
一、創(chuàng)建聯(lián)系人
1、創(chuàng)建聯(lián)系人記錄
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);例:
// 創(chuàng)建可變多值類型對象 ABMutableMultiValueRef multi = ABMultiValueCreateMutable(kABMultiStringProper); // 設(shè)置電話號碼 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、添加記錄到數(shù)據(jù)庫
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);?二、修改聯(lián)系人信息
1、獲取聯(lián)系人記錄
ABRecordRef ABAddressBookGetPersonWithRecordID(ABAddressBookRef addressBook,ABRecordID recordID);三、刪除聯(lián)系人信息
bool ABAddressBookRemoveRecord(ABAddressBookRef addressBook,ABRecordRef record,CFErrorRef *error);?四、實戰(zhàn)代碼
#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);}}); } // 刪除聯(lián)系人后 重新加載列表信息 - (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 = 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);// 設(shè)置電話號碼ABMutableMultiValueRef multi = ABMultiValueCreateMutable(kABMultiStringPropertyType);ABMultiValueAddValueAndLabel(multi, (__bridge CFTypeRef)self.txtMobile.text,kABPersonPhoneMobileLabel, NULL);ABMultiValueAddValueAndLabel(multi, (__bridge CFTypeRef)self.txtIPhone.text,kABPersonPhoneIPhoneLabel, NULL);// 添加電話號碼到數(shù)據(jù)庫ABRecordSetValue(person, kABPersonPhoneProperty, multi, &error);CFRelease(multi);// 設(shè)置Email屬性multi = ABMultiValueCreateMutable(kABMultiStringPropertyType);ABMultiValueAddValueAndLabel(multi, (__bridge CFTypeRef)self.txtHomeEmail.text,kABHomeLabel, NULL);ABMultiValueAddValueAndLabel(multi, (__bridge CFTypeRef)self.txtWorkEmail.text,kABWorkLabel, NULL);// 添加Email到數(shù)據(jù)庫ABRecordSetValue(person, kABPersonEmailProperty, multi, &error);CFRelease(multi);// 保存到數(shù)據(jù)庫ABAddressBookSave(addressBook, &error);CFRelease(addressBook);//導(dǎo)航回根視圖控制器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);// 保存到數(shù)據(jù)庫ABAddressBookSave(addressBook, &error);CFRelease(addressBook);//導(dǎo)航回根視圖控制器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);// 設(shè)置電話號碼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);// 設(shè)置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);// 增加記錄到數(shù)據(jù)庫ABAddressBookAddRecord(addressBook, person, &error);// 保存到數(shù)據(jù)庫ABAddressBookSave(addressBook, &error);CFRelease(person);CFRelease(addressBook);[self dismissViewControllerAnimated:YES completion:nil];}- (IBAction)cancelClick:(id)sender {[self dismissViewControllerAnimated:YES completion:nil]; }@end?
?
轉(zhuǎn)載于:https://www.cnblogs.com/cqchen/p/3803254.html
總結(jié)
以上是生活随笔為你收集整理的iOS.访问通讯录.02.写入联系人的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: [转载]每个极客都应该知道的Linux技
- 下一篇: vim for python