簡(jiǎn)單的對(duì)于通訊錄獲得的總結(jié)苛坚,不成敬意予权!
1.用的類庫(kù),導(dǎo)入的頭文件
iOS9之前
#import <AddressBook/AddressBook.h>
#import <AddressBookUI/AddressBookUI.h>
遵循
ABPeoplePickerNavigationControllerDelegate
iOS9之后
#import <Contacts/Contacts.h>
#import <ContactsUI/ContactsUI.h>
遵循
CNContactPickerDelegate
2.僅僅讀取(相當(dāng)于調(diào)用系統(tǒng)的類和界面(類似獲取照片的功能))
(1)是否授權(quán)方面:(示例代碼)
iOS9之前
if (IOS_BEFORE(9.0)) {
WS(ws);
ABAuthorizationStatus status = ABAddressBookGetAuthorizationStatus();
if (status ==kABAuthorizationStatusAuthorized) {// 授權(quán)成功
ABPeoplePickerNavigationController *peoplePicker = [[ABPeoplePickerNavigationController alloc] init];
peoplePicker.peoplePickerDelegate = self;
// iOS8之后要加入這個(gè)屬性
if (IOS_LATER(8.0)) {
peoplePicker.predicateForSelectionOfPerson = [NSPredicate predicateWithValue:false];
}
[self presentViewController:peoplePicker animated:YES completion:nil];
}else {// 授權(quán)失敗
// 獲取通訊錄不是在主線程中獲得,所以切換主線程,展示錯(cuò)誤
dispatch_async(dispatch_get_main_queue(), ^{
[ws antuorFaildToShowView];
});
NSLog(@"授權(quán)失敗");
}
}
iOS9之后
else {
CNContactStore *contactStore = [[CNContactStore alloc] init];
WS(ws);
[contactStore requestAccessForEntityType:CNEntityTypeContacts completionHandler:^(BOOL granted, NSError * _Nullable error) {
if (granted) {
CNContactPickerViewController *picker = [[CNContactPickerViewController alloc] init];
picker.delegate = self;
[self presentViewController:picker animated:YES completion:^{}];
}else {
NSLog(@"授權(quán)失敗");
NSLog(@"[NSThread currentThread] = %@",[NSThread currentThread]) ;
// 獲取通訊錄不是在主線程中獲得备蚓,所以切換主線程课蔬,展示錯(cuò)誤
dispatch_async(dispatch_get_main_queue(), ^{
[ws antuorFaildToShowView];
});
}
}];
}
(2)代理方法示例:(示例代碼)
iOS9之前
#pragma mark - ABPeoplePickerNavigationControllerDelegate
- (void)peoplePickerNavigationController:(ABPeoplePickerNavigationController *)peoplePicker didSelectPerson:(ABRecordRef)person property:(ABPropertyID)property identifier:(ABMultiValueIdentifier)identifier{
ABMultiValueRef valuesRef = ABRecordCopyValue(person, kABPersonPhoneProperty);
CFIndex index = ABMultiValueGetIndexForIdentifier(valuesRef,identifier);
CFStringRef value = ABMultiValueCopyValueAtIndex(valuesRef,index);
NSString *phoneNumber = (__bridge NSString*)value;
NSString *newPhone =[self trimSting:phoneNumber];
//賦值給我TextFeild
[self saveTeleToLoca:newPhone];
[self dismissViewControllerAnimated:YES completion:^{}];
}
-(void)saveTeleToLoca:(NSString *)phone {
_phoneTF.text =phone;
[CMUserDefaults saveLocalString:phone andKey:kPhoneRechargeTelePhone];
}
- (void)peoplePickerNavigationControllerDidCancel:(ABPeoplePickerNavigationController *)peoplePicker {
[self dismissViewControllerAnimated:YES completion:nil];
}
iOS9之后
#pragma mark - CNContactPickerDelegate
#ifdef __IPHONE_9_0
// 通訊錄列表 - 點(diǎn)擊某個(gè)聯(lián)系人 - 詳情頁(yè) - 點(diǎn)擊一個(gè)號(hào)碼, 返回
- (void)contactPicker:(CNContactPickerViewController *)picker didSelectContactProperty:(CNContactProperty *)contactProperty {
// 獲得到用戶的手機(jī)號(hào)碼
NSString *phoneNumber = [contactProperty.value stringValue];
// 去掉手機(jī)號(hào)碼的“-”
NSString *newPhone =[self trimSting:phoneNumber];
//賦值給我TextFeild
[self saveTeleToLoca:newPhone];
}
- (void)contactPickerDidCancel:(CNContactPickerViewController *)picker {
[self dismissViewControllerAnimated:YES completion:nil];
}
#endif
注意:
1.iOS10之后要在info.plist 中加入以下的屬性(來(lái)詢問獲得用戶的權(quán)限)
<key>NSContactsUsageDescription</key>
<string>contactsDesciption</string>
具體iOS10之后相冊(cè)和麥克風(fēng)都要征得用戶權(quán)限的key詳見:http://www.reibang.com/p/c212cde86877
2.對(duì)于為了防止iOS9的系統(tǒng)運(yùn)行出現(xiàn)iOS10的程序防止編譯錯(cuò)誤囱稽,無(wú)法通過(guò),應(yīng)該用條件編譯如下:
#if __IPHONE_9_0
// 9.0之后的用二跋,防止9.0之前的編譯失敗
#import <Contacts/Contacts.h>
#import <ContactsUI/ContactsUI.h>
@interface PhoneRechargeViewController ()<UITextFieldDelegate,CNContactPickerDelegate,NHThirdPayViewDelegate,ABPeoplePickerNavigationControllerDelegate>
#else
@interface PhoneRechargeViewController ()<UITextFieldDelegate,ABPeoplePickerNavigationControllerDelegate,NHThirdPayViewDelegate>
#endif
3.需要獲得通訊錄具體數(shù)據(jù)用:(相當(dāng)于獲得通訊錄數(shù)據(jù)战惊,自己展示)
后續(xù)總結(jié):
參考網(wǎng)址:
http://www.reibang.com/p/6acad14cf3c9
http://blog.csdn.net/kenrry1992/article/details/51252274
http://www.reibang.com/p/df0ea100c3da