首页 > 代码库 > iOS开发之系统通讯录
iOS开发之系统通讯录
@iOS调用操作通讯录所用的库文件
AddressBook.framework
AddressBookUI.framework
#import "HMTMainViewController.h" #import <AddressBook/AddressBook.h> #import <AddressBookUI/AddressBookUI.h> @interface HMTMainViewController ()<ABPeoplePickerNavigationControllerDelegate> @property (nonatomic,strong) ABPeoplePickerNavigationController *personPickVC; @property (nonatomic,strong) UIWebView *phoneCallWebView; @end @implementation HMTMainViewController - (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil { self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil]; if (self) { // Custom initialization } return self; } - (void)viewDidLoad { [super viewDidLoad]; // Do any additional setup after loading the view. self.navigationItem.title = @"系统通讯录"; // self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"删除" style:UIBarButtonItemStylePlain target:self action:@selector(didClickDeletePersonAction)]; UIButton *button = [UIButton buttonWithType:UIButtonTypeSystem]; button.frame = CGRectMake(100, 70, 120, 40); [button setTitle:@"选择联系人" forState:UIControlStateNormal]; [button addTarget:self action:@selector(didClickSelectAction) forControlEvents:UIControlEventTouchUpInside]; [self.view addSubview:button]; } - (void)didClickSelectAction{ // 选择电话薄的联系人 self.personPickVC = [[ABPeoplePickerNavigationController alloc] init]; _personPickVC.peoplePickerDelegate = self; _personPickVC.displayedProperties = @[[NSNumber numberWithInt:kABPersonPhoneProperty]]; [self deletePerson]; [self presentViewController:_personPickVC animated:YES completion:NULL]; //[self.navigationController pushViewController:[_personPickVC.viewControllers objectAtIndex:0] animated:YES]; //[self getAllPerson]; } // 进入选择联系人界面 - (BOOL)peoplePickerNavigationController:(ABPeoplePickerNavigationController *)peoplePicker shouldContinueAfterSelectingPerson:(ABRecordRef)person{ // 选中联系人后,就退出,并返回想要的属性 [peoplePicker dismissViewControllerAnimated:YES completion:^{ NSString *firstName = (__bridge NSString*)ABRecordCopyValue(person, kABPersonFirstNameProperty); NSLog(@"person = %@",firstName); ABMultiValueRef phones = ABRecordCopyValue(person,kABPersonPhoneProperty); for (int i = 0; i < ABMultiValueGetCount(phones); i++) { NSString *phone = (__bridge NSString *)(ABMultiValueCopyValueAtIndex(phones, i)); NSLog(@"telephone = %@",phone); } }]; return YES; } - (void)peoplePickerNavigationControllerDidCancel:(ABPeoplePickerNavigationController *)peoplePicker{ [peoplePicker dismissViewControllerAnimated:YES completion:^{ }]; } - (BOOL)peoplePickerNavigationController:(ABPeoplePickerNavigationController *)peoplePicker shouldContinueAfterSelectingPerson:(ABRecordRef)person property:(ABPropertyID)property identifier:(ABMultiValueIdentifier)identifier{ return YES; } // 展示所有联系人 - (void)getAllPerson{ CFArrayRef allPerson = ABAddressBookCopyArrayOfAllPeople(self.personPickVC.addressBook); for (id person in ((__bridge NSArray *)allPerson)) { NSString *firstName = (__bridge NSString*)ABRecordCopyValue((__bridge ABRecordRef)person, kABPersonFirstNameProperty); NSLog(@"firstName = %@",firstName); // 因为一个用户可能有多个电话,所以需要循环调取 ABMultiValueRef phones = ABRecordCopyValue((__bridge ABRecordRef)person,kABPersonPhoneProperty); for (int i = 0; i < ABMultiValueGetCount(phones); i++) { NSString *phone = (__bridge NSString *)(ABMultiValueCopyValueAtIndex(phones, i)); NSLog(@"telephone = %@",phone); } } } // 添加联系人 - (void)addPerson{ ABAddressBookRef addressBook = self.personPickVC.addressBook; ABRecordRef person = ABPersonCreate(); ABRecordSetValue(person, kABPersonFirstNameProperty, @"胡", nil); ABRecordSetValue(person, kABPersonLastNameProperty, @"明涛", nil); ABMutableMultiValueRef mulRef = ABMultiValueCreateMutable(kABStringPropertyType); for (int i = 0; i < 1; i++) { ABMultiValueIdentifier mutableIdentifier; ABMultiValueAddValueAndLabel(mulRef, @"18690231234", nil, &mutableIdentifier); } ABRecordSetValue(person, kABPersonPhoneProperty, mulRef, nil); ABAddressBookAddRecord(addressBook, person, nil); ABAddressBookSave(addressBook, nil); } // 删除联系人 - (void)deletePerson{ CFArrayRef allPerson = ABAddressBookCopyArrayOfAllPeople(self.personPickVC.addressBook); for (id person in (__bridge NSArray *)allPerson) { NSString *firstName = (__bridge NSString*)ABRecordCopyValue((__bridge ABRecordRef)person, kABPersonFirstNameProperty); if ([firstName isEqualToString:@"胡"]) { ABAddressBookRemoveRecord(self.personPickVC.addressBook, (__bridge ABRecordRef)person, nil); } } ABAddressBookSave(self.personPickVC.addressBook, nil); } // 拨打电话 - (void)dialPhoneNumber:(NSString *)phoneNumber{ // 1.UIWebView加载电话 NSURL *phoneURL = [NSURL URLWithString:[NSString stringWithFormat:@"tel:%@",phoneNumber]]; self.phoneCallWebView = [[UIWebView alloc] initWithFrame:CGRectZero]; [self.phoneCallWebView loadRequest:[NSURLRequest requestWithURL:phoneURL]]; // 2.私有方法 [[UIApplication sharedApplication] openURL:[NSURL URLWithString:@"telprompt://10086"]]; }
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。