您的位置:首页 > 移动开发 > IOS开发

iOS通讯录iOS9,8,7

2016-04-13 20:11 239 查看
````

原文链接 :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:这是每个应用软件期望得到的状态。在该状态下,应用软件可以随意访问通讯录数据库,使用通讯录数据执行操作。

我们其实只需要判断授权状态是否为 CNAuthorizationStatusAuthorized 即可,如果是表示授权状态成功,否则失败,不允许访问通讯录。
CNContactStore (相当于ABAddressBook)类以编程方式展示了联系人数据库,并且提供了许多实现不同任务的方法,例如获取,保存或者更新记录,权限检查和权限请求,很多很多。
CNContact类(相当于ABRecordRef)展示一个单独的联系人记录,但是记住这个类的特性是不可变的。如果你想创建一个新的联系人记录或者更新一个已存在的联系人记录,你必须使用CNMutableContact类。

#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中,导入

import <AddressBookUI/AddressBookUI.h>,遵守协议并实现方法,略麻烦

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差别不大

#pragma mark - ios8走这个 选中联系人的某个属性的时候调用
- (void)peoplePickerNavigationController:(ABPeoplePickerNavigationController*)peoplePicker didSelectPerson:(ABRecordRef)person property:(ABPropertyID)property identifier:(ABMultiValueIdentifier)identifier
{
//与7一样,蝙蝠问题省略
}

/*(a)peoplePickerNavigationControllerDidCancel:当用户选择取消时调用这个方法,可以在这个方法里取消整个通讯录页面的显示。

(b)peoplePickerNavigationController:shouldContinueAfterSelectingPerson: 当用户选择了通讯录中某一个联系人时调用这个方法,可以在这里获取联系人的信息。如果希望可以继续显示这个联系人更具体的信息,则return YES。否则取消整个通讯录页面的显示并return NO。

(c)peoplePickerNavigationController:shouldContinueAfterSelectingPerson:property:identifier: 如果上一个方法返回的是YES,则会显示某一个联系人信息,如果选择了联系人的某一项纪录,就会调用这个方法,可以通过点击选择联系人的某一项信息。如果 希望可以对选择的某一项纪录进行进一步操作,比如直接拨打电话或调用邮箱发送邮件,则return YES。否则取消整个通讯录页面的显示并return NO。*/
未完持续,技术是一天天积累的,加油

````
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: