````ios
原文連接 :http://www.jianshu.com/p/64951a543efc 數據庫
實現相似京東地址編輯時選取通信錄後獲得電話和名字.這裏將分別實現iOS7,8,9,但願對你有幫助,如有幫助請點喜歡,如有疑問請評論,讓我看到你的雙手謝謝.原創文章---lzc.
先說iOS9:導入#import <ContactsUI/ContactsUI.h>
實現CNContactPickerDelegate協議方法.編程
//讓用戶給權限,沒有的話會被拒的各位 CNAuthorizationStatus status = [CNContactStore authorizationStatusForEntityType:CNEntityTypeContacts]; if (status == CNAuthorizationStatusNotDetermined) { CNContactStore *store = [[CNContactStore alloc] init]; [store requestAccessForEntityType:CNEntityTypeContacts completionHandler:^(BOOL granted, NSError * _Nullable error) { if (error) { NSLog(@"weishouquan "); }else { NSLog(@"chenggong ");//用戶給權限了 CNContactPickerViewController * picker = [CNContactPickerViewController new]; picker.delegate = self; picker.displayedPropertyKeys = @[CNContactPhoneNumbersKey];//只顯示手機號 [self presentViewController: picker animated:YES completion:nil]; } }]; } if (status == CNAuthorizationStatusAuthorized) {//有權限時 CNContactPickerViewController * picker = [CNContactPickerViewController new]; picker.delegate = self; picker.displayedPropertyKeys = @[CNContactPhoneNumbersKey]; [self presentViewController: picker animated:YES completion:nil]; } else{ @"您未開啓通信錄權限,請前往設置中心開啓"; }
authorizationStatusForEntityType:methods這個方法是CNContactStore的類方法,須要一個 CNEntityType 參數,返回值是受權獲得的狀態CNAuthorizationStatus,一共有四種,分別爲:
NotDetermined:表示用戶尚未容許或禁止訪問通信錄數據庫。首次安裝的應用軟件處於這種狀態。
Restricted:不只應用軟件沒法訪問通信錄數據,就連用戶也沒法經過設置修改受權狀態。該狀態是因爲其餘限制,也就是家長控制(parental control)所致使。
Denied:表示用戶不容許訪問通信錄數據。只有用戶纔可以修改該狀態。
Authorized:這是每一個應用軟件指望獲得的狀態。在該狀態下,應用軟件能夠隨意訪問通信錄數據庫,使用通信錄數據執行操做。ide
咱們其實只須要判斷受權狀態是否爲 CNAuthorizationStatusAuthorized 便可,若是是表示受權狀態成功,不然失敗,不容許訪問通信錄。
CNContactStore (至關於ABAddressBook)類以編程方式展現了聯繫人數據庫,而且提供了許多實現不一樣任務的方法,例如獲取,保存或者更新記錄,權限檢查和權限請求,不少不少。
CNContact類(至關於ABRecordRef)展現一個單獨的聯繫人記錄,可是記住這個類的特性是不可變的。若是你想建立一個新的聯繫人記錄或者更新一個已存在的聯繫人記錄,你必須使用CNMutableContact類。ui
#pragma mark - 點擊某個聯繫人的某個屬性(property)時觸發並返回該聯繫人屬性(contactProperty)。 //只實現該方法時,能夠進入到聯繫人詳情頁面(若是predicateForSelectionOfProperty屬性沒被設置或符合篩選條件,如不符合會觸發默認操做,即打電話,發郵件等)。 - (void)contactPicker:(CNContactPickerViewController *)picker didSelectContactProperty:(CNContactProperty *)contactProperty { NSLog(@"%@",contactProperty); CNContact *contact = contactProperty.contact; NSLog(@"givenName: %@, familyName: %@", contact.givenName, contact.familyName); self.nameTextView.text = [NSString stringWithFormat:@"%@%@",contact.familyName,contact.givenName]; if (![contactProperty.value isKindOfClass:[CNPhoneNumber class]]) { [[HNPublicTool shareInstance] showHudErrorMessage:@"請選擇11位手機號"]; return; } CNPhoneNumber *phoneNumber = contactProperty.value; NSString * Str = phoneNumber.stringValue; NSCharacterSet *setToRemove = [[ NSCharacterSet characterSetWithCharactersInString:@"0123456789"]invertedSet]; NSString *phoneStr = [[Str componentsSeparatedByCharactersInSet:setToRemove]componentsJoinedByString:@""]; if (phoneStr.length != 11) { [[HNPublicTool shareInstance] showHudErrorMessage:@"請選擇11位手機號"]; } NSLog(@"-=-=%@",phoneStr); self.phoneTextView.text = phoneStr; }
未完待續,新技術資料較少,花了很多精力,謝謝幫助個人大神,博採衆長,方得此篇...
更新以下
iOS7,8中,導入spa
ABPeoplePickerNavigationControllerDelegate代理
- (void)visitAddressBook {//受權 __weak typeof(self)weakSelf = self; ABAddressBookRef bookref = ABAddressBookCreateWithOptions(NULL, NULL); ABAuthorizationStatus status = ABAddressBookGetAuthorizationStatus(); /*kABAuthorizationStatusNotDetermined = 0, // 未進行受權選擇 kABAuthorizationStatusRestricted, // 未受權,且用戶沒法更新,如家長控制狀況下 kABAuthorizationStatusDenied, // 用戶拒絕App使用 kABAuthorizationStatusAuthorized // 已受權,可以使用*/ if (status == kABAuthorizationStatusNotDetermined) { ABAddressBookRequestAccessWithCompletion(bookref, ^(bool granted, CFErrorRef error) { if (error) { NSLog(@"受權錯誤"); } if (granted) { NSLog(@"受權chengg"); ABPeoplePickerNavigationController *peosonVC = [[ABPeoplePickerNavigationController alloc] init]; peosonVC.peoplePickerDelegate = weakSelf; peosonVC.displayedProperties = @[[NSNumber numberWithInt:kABPersonPhoneProperty]]; [weakSelf presentViewController:peosonVC animated:YES completion:nil]; } }); } if (status == kABAuthorizationStatusAuthorized) { ABPeoplePickerNavigationController *peosonVC = [[ABPeoplePickerNavigationController alloc] init]; peosonVC.peoplePickerDelegate = weakSelf; peosonVC.displayedProperties = @[[NSNumber numberWithInt:kABPersonPhoneProperty]]; [weakSelf presentViewController:peosonVC animated:YES completion:nil]; }else { @"您未開啓通信錄權限,請前往設置中心開啓"]; } }
#pragma mark iOS7通信錄代理方法 //取消選擇 7上必須有,不然崩lzc,切記 - (void)peoplePickerNavigationControllerDidCancel:(ABPeoplePickerNavigationController *)peoplePicker { [peoplePicker dismissViewControllerAnimated:YES completion:nil]; } - (BOOL)peoplePickerNavigationController:(ABPeoplePickerNavigationController *)peoplePicker shouldContinueAfterSelectingPerson:(ABRecordRef)person { return YES; } - (BOOL)peoplePickerNavigationController:(ABPeoplePickerNavigationController *)peoplePicker shouldContinueAfterSelectingPerson:(ABRecordRef)person property:(ABPropertyID)property identifier:(ABMultiValueIdentifier)identifier { // 獲取該聯繫人多重屬性--電話號 ABMutableMultiValueRef phoneMulti = ABRecordCopyValue(person, kABPersonPhoneProperty); // 獲取該聯繫人的名字,簡單屬性,只需ABRecordCopyValue取一次值 ABMutableMultiValueRef firstName = ABRecordCopyValue(person, kABPersonFirstNameProperty); NSString *firstname = (__bridge NSString *)(firstName); ABMutableMultiValueRef lastName = ABRecordCopyValue(person, kABPersonLastNameProperty); NSString *lastname = (__bridge NSString *)(lastName); // 獲取點擊的聯繫人的電話 NSLog(@"聯繫人名字 : %@%@",lastname,firstname); // 點擊某個聯繫人電話後dismiss聯繫人控制器,並回調點擊的數據 [self dismissViewControllerAnimated:YES completion:^{ // 從多重屬性——電話號中取值,參數2是取點擊的索引 NSString *aPhone = (__bridge_transfer NSString *)ABMultiValueCopyValueAtIndex(phoneMulti, ABMultiValueGetIndexForIdentifier(phoneMulti,identifier)) ; // 獲取點擊的聯繫人的電話,也能夠取標籤等 NSLog(@"聯繫人電話 : %@",aPhone); // 去掉電話號中的 "-" aPhone = [aPhone stringByReplacingOccurrencesOfString:@"-" withString:@"" ]; NSLog(@"去掉-號 : %@",aPhone); }]; return NO;//若是不返回NO,會有別的效果,但願你動動手,我就不告訴你--LZC }
iOS8與7差異不大code
#pragma mark - ios8走這個 選中聯繫人的某個屬性的時候調用 - (void)peoplePickerNavigationController:(ABPeoplePickerNavigationController*)peoplePicker didSelectPerson:(ABRecordRef)person property:(ABPropertyID)property identifier:(ABMultiValueIdentifier)identifier { //與7同樣,蝙蝠問題省略 }
/*(a)peoplePickerNavigationControllerDidCancel:當用戶選擇取消時調用這個方法,能夠在這個方法裏取消整個通信錄頁面的顯示。component
(b)peoplePickerNavigationController:shouldContinueAfterSelectingPerson: 當用戶選擇了通信錄中某一個聯繫人時調用這個方法,能夠在這裏獲取聯繫人的信息。若是但願能夠繼續顯示這個聯繫人更具體的信息,則return YES。不然取消整個通信錄頁面的顯示並return NO。orm
(c)peoplePickerNavigationController:shouldContinueAfterSelectingPerson:property:identifier: 若是上一個方法返回的是YES,則會顯示某一個聯繫人信息,若是選擇了聯繫人的某一項紀錄,就會調用這個方法,能夠經過點擊選擇聯繫人的某一項信息。若是 但願能夠對選擇的某一項紀錄進行進一步操做,好比直接撥打電話或調用郵箱發送郵件,則return YES。不然取消整個通信錄頁面的顯示並return NO。*/
未完持續,技術是一每天積累的,加油
````