#import <ContactsUI/ContactsUI.h>
<CNContactPickerDelegate>
// 判断当前的授权状态
if (status != CNAuthorizationStatusAuthorized) {
UIAlertView * alart = [[UIAlertView alloc]initWithTitle:@"温馨提示" message:@"请您设置允许APP访问您的通讯录\n设置-隐私-通讯录" delegate:self cancelButtonTitle:@"确定" otherButtonTitles:nil, nil];
return;
}
typedef NS_ENUM(NSInteger, CNAuthorizationStatus)
{
/*! 用户尚未就应用程序是否可以访问联系人数据做出选择。 */
CNAuthorizationStatusNotDetermined = 0,
/*! 该应用程序没有权限访问联系人数据。
*用户无法更改此应用程序的状态,可能是由于主动限制(如父母控制到位)。 */
CNAuthorizationStatusRestricted,
/*! 用户明确拒绝对应用程序的联系人数据的访问。 */
CNAuthorizationStatusDenied,
/*! 该应用程序被授权访问联系人数据。 */
CNAuthorizationStatusAuthorized
}
// 判断当前的授权状态是否是用户还未选择的状态
if (status == CNAuthorizationStatusNotDetermined)
{
CNContactStore *store = [CNContactStore new];
[store requestAccessForEntityType:CNEntityTypeContacts completionHandler:^(BOOL granted, NSError * _Nullable error) {
if (granted){
NSLog(@"授权成功!");
}else{
NSLog(@"授权失败!");
}
}];
}
-(void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
//iOS 10
// AB_DEPRECATED("Use CNContactPickerViewController from ContactsUI.framework instead")
CNContactPickerViewController * contactVc = [CNContactPickerViewController new];
contactVc.delegate = self;
[self presentViewController:contactVc animated:YES completion:nil];
}
// 选择某个联系人时调用
- (void)contactPicker:(CNContactPickerViewController *)picker didSelectContactProperty:(CNContactProperty *)contactProperty
{
CNContact *contact = contactProperty.contact;
NSString *name = [CNContactFormatter stringFromContact:contact style:CNContactFormatterStyleFullName];
CNPhoneNumber *phoneValue= contactProperty.value;
NSString *phoneNumber = phoneValue.stringValue;
NSLog(@"%@--%@",name, phoneNumber);
}
// 1.选择联系人时使用(不展开详情) - (void)contactPicker:(CNContactPickerViewController *)picker didSelectContact:(CNContact *)contact; 注:如果有上面的方法,下面的方法不执行 // 2.选择联系人某个属性时调用(展开详情) - (void)contactPicker:(CNContactPickerViewController *)picker didSelectContactProperty:(CNContactProperty *)contactProperty; // 3.取消选中联系人时调用 - (void)contactPickerDidCancel:(CNContactPickerViewController *)picker;
- (void)contactPickerDidCancel:(CNContactPickerViewController *)picker{
[picker dismissViewControllerAnimated:YES completion:nil];
}
// 创建通讯录对象
CNContactStore *contactStore = [CNContactStore new];
NSArray *keys = @[CNContactPhoneNumbersKey,CNContactGivenNameKey];
// 获取通讯录中所有的联系人
CNContactFetchRequest *request = [[CNContactFetchRequest alloc] initWithKeysToFetch:keys];
[contactStore enumerateContactsWithFetchRequest:request error:nil usingBlock:^(CNContact * _Nonnull contact, BOOL * _Nonnull stop) {
// 获取姓名
// NSString *firstName = contact.familyName;
NSString *lastName = contact.givenName;
NSLog(@"name: %@",lastName);
// 获取电话号码
for (CNLabeledValue *labeledValue in contact.phoneNumbers){
CNPhoneNumber *phoneValue = labeledValue.value;
NSString *phoneNumber = phoneValue.stringValue;
NSLog(@"number: %@",phoneNumber);
}
}];
机械节能产品生产企业官网模板...
大气智能家居家具装修装饰类企业通用网站模板...
礼品公司网站模板
宽屏简约大气婚纱摄影影楼模板...
蓝白WAP手机综合医院类整站源码(独立后台)...苏ICP备2024110244号-2 苏公网安备32050702011978号 增值电信业务经营许可证编号:苏B2-20251499 | Copyright 2018 - 2025 源码网商城 (www.ymwmall.com) 版权所有