获取系统的通讯录,自定义界面
#import "ViewController.h"
#import <AddressBook/AddressBook.h>
@interface ViewController ()
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
[self getAuthor];
}
- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
if (ABAddressBookGetAuthorizationStatus() != kABAuthorizationStatusAuthorized) {
return;
}
ABAddressBookRef bookRef = ABAddressBookCreate();
CFArrayRef array = ABAddressBookCopyArrayOfAllPeople(bookRef);
CFIndex count = CFArrayGetCount(array);
//遍历所有人
for (int index = 0; index < count; index++) {
ABRecordRef recordRef = CFArrayGetValueAtIndex(array, index);
NSString * firstName = (__bridge_transfer NSString *)ABRecordCopyValue(recordRef, kABPersonFirstNameProperty);
NSString * lastName = (__bridge_transfer NSString *)ABRecordCopyValue(recordRef, kABPersonLastNameProperty);
// NSLog(@"%@---%@",firstName,lastName);
NSLog(@"%@",lastName);
// 获取电话号码
ABMultiValueRef multiValue = ABRecordCopyValue(recordRef, kABPersonPhoneProperty);
CFIndex count = ABMultiValueGetCount(multiValue);
for (int i = 0; i < count; i ++) {
NSString *label = (__bridge_transfer NSString *)ABMultiValueCopyLabelAtIndex(multiValue, i);
NSString *phone = (__bridge_transfer NSString *)ABMultiValueCopyValueAtIndex(multiValue, i);
// NSLog(@"%@---%@", label, phone);
}
CFRelease(multiValue);
}
CFRelease(bookRef);
CFRelease(array);
}
- (void)getAuthor{
if (ABAddressBookGetAuthorizationStatus() == kABAuthorizationStatusNotDetermined){
ABAddressBookRef bookRef = ABAddressBookCreate();
ABAddressBookRequestAccessWithCompletion(bookRef, ^(bool granted, CFErrorRef error) {
if (granted) {
NSLog(@"授权成功!");
}
else
{
NSLog(@"授权失败!");
}
});
}
// kABAuthorizationStatusNotDetermined = 0, // deprecated, use CNAuthorizationStatusNotDetermined
// kABAuthorizationStatusRestricted, // deprecated, use CNAuthorizationStatusRestricted
// kABAuthorizationStatusDenied, // deprecated, use CNAuthorizationStatusDenied
// kABAuthorizationStatusAuthorized
}
@end
网友评论