@iOS叫用作業通訊錄所用的庫檔案
AddressBook.framework
AddressBookUI.framework
#import "HMTMainViewController.h"#import #import @interface HMTMainViewController ()@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"]];}