iOS本地通讯录获取

简单的对于通讯录获得的总结,不成敬意!
1.用的类库,导入的头文件

iOS9之前

#import 
#import 
遵循 
ABPeoplePickerNavigationControllerDelegate

iOS9之后

#import 
#import 
遵循 
CNContactPickerDelegate

2.仅仅读取(相当于调用系统的类和界面(类似获取照片的功能))

(1)是否授权方面:(示例代码)

iOS9之前

if (IOS_BEFORE(9.0)) {
        WS(ws);
        ABAuthorizationStatus status = ABAddressBookGetAuthorizationStatus();
        if (status ==kABAuthorizationStatusAuthorized) {// 授权成功
            ABPeoplePickerNavigationController *peoplePicker = [[ABPeoplePickerNavigationController alloc] init];
            peoplePicker.peoplePickerDelegate = self;
            // iOS8之后要加入这个属性
            if (IOS_LATER(8.0)) {
                peoplePicker.predicateForSelectionOfPerson = [NSPredicate predicateWithValue:false];
            }
            [self presentViewController:peoplePicker animated:YES completion:nil];
        }else {// 授权失败
            // 获取通讯录不是在主线程中获得,所以切换主线程,展示错误
            dispatch_async(dispatch_get_main_queue(), ^{
                [ws antuorFaildToShowView];
            });
            NSLog(@"授权失败");
        }
       
    }

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(@"授权失败");
               NSLog(@"[NSThread currentThread] = %@",[NSThread currentThread]) ;
                // 获取通讯录不是在主线程中获得,所以切换主线程,展示错误
                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

// 通讯录列表 - 点击某个联系人 - 详情页 - 点击一个号码, 返回
- (void)contactPicker:(CNContactPickerViewController *)picker didSelectContactProperty:(CNContactProperty *)contactProperty {
    
    // 获得到用户的手机号码
    NSString *phoneNumber = [contactProperty.value stringValue];
    // 去掉手机号码的“-”
    NSString  *newPhone =[self trimSting:phoneNumber];
    
    //赋值给我TextFeild
    [self saveTeleToLoca:newPhone];
    
}
- (void)contactPickerDidCancel:(CNContactPickerViewController *)picker {
    [self dismissViewControllerAnimated:YES completion:nil];
}
#endif

注意:
1.iOS10之后要在info.plist 中加入以下的属性(来询问获得用户的权限)

    NSContactsUsageDescription    
    contactsDesciption

具体iOS10之后相册和麦克风都要征得用户权限的key详见:http://www.jianshu.com/p/c212cde86877

2.对于为了防止iOS9的系统运行出现iOS10的程序防止编译错误,无法通过,应该用条件编译如下:

#if __IPHONE_9_0
// 9.0之后的用,防止9.0之前的编译失败
#import 
#import 
@interface PhoneRechargeViewController ()
#else

@interface PhoneRechargeViewController ()
#endif

3.需要获得通讯录具体数据用:(相当于获得通讯录数据,自己展示)
后续总结:

参考网址:
http://www.jianshu.com/p/6acad14cf3c9
http://blog.csdn.net/kenrry1992/article/details/51252274
http://www.jianshu.com/p/df0ea100c3da

你可能感兴趣的:(iOS本地通讯录获取)