#import "ViewController.h"
#import <AddressBook/AddressBook.h>
@interface ViewController ()
@end
@implementation ViewController
- (
void
)viewDidLoad {
[super viewDidLoad];
}
- (
void
)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event {
ABAuthorizationStatus authorizationStatus = ABAddressBookGetAuthorizationStatus();
if
(authorizationStatus != kABAuthorizationStatusAuthorized) {
NSLog(@
"没有授权"
);
return
;
}
ABAddressBookRef addressBookRef = ABAddressBookCreate();
CFArrayRef arrayRef = ABAddressBookCopyArrayOfAllPeople(addressBookRef);
long
count = CFArrayGetCount(arrayRef);
for
(
int
i = 0; i < count; i++) {
ABRecordRef people = CFArrayGetValueAtIndex(arrayRef, i);
NSString *firstName=(__bridge NSString *)(ABRecordCopyValue(people, kABPersonFirstNameProperty));
NSString *lastName=(__bridge NSString *)(ABRecordCopyValue(people, kABPersonLastNameProperty));
NSLog(@
"--------------------------------------------------"
);
NSLog(@
"firstName=%@, lastName=%@"
, firstName, lastName);
NSMutaleArray *phoneArray = [[NSMutableArray alloc]init];
ABMultiValueRef phones = ABRecordCopyValue(people, kABPersonPhoneProperty);
for
(NSInteger j=0; j<ABMultiValueGetCount(phones); j++) {
NSString *phone = (__bridge NSString *)(ABMultiValueCopyValueAtIndex(phones, j));
NSLog(@
"phone=%@"
, phone);
[phoneArray addObject:phone];
}
NSMutableArray *emailArray = [[NSMutableArray alloc]init];
ABMultiValueRef emails= ABRecordCopyValue(people, kABPersonEmailProperty);
for
(NSInteger j=0; j<ABMultiValueGetCount(emails); j++) {
NSString *email = (__bridge NSString *)(ABMultiValueCopyValueAtIndex(emails, j));
NSLog(@
"email=%@"
, email);
[emailArray addObject:email];
}
NSString *middleName=(__bridge NSString*)(ABRecordCopyValue(people, kABPersonMiddleNameProperty));
NSString *prefix=(__bridge NSString*)(ABRecordCopyValue(people, kABPersonPrefixProperty));
NSString *suffix=(__bridge NSString*)(ABRecordCopyValue(people, kABPersonSuffixProperty));
NSString *nickName=(__bridge NSString*)(ABRecordCopyValue(people, kABPersonNicknameProperty));
NSString *firstNamePhoneic=(__bridge NSString*)(ABRecordCopyValue(people, kABPersonFirstNamePhoneticProperty));
NSString *lastNamePhoneic=(__bridge NSString*)(ABRecordCopyValue(people, kABPersonLastNamePhoneticProperty));
NSString *middleNamePhoneic=(__bridge NSString*)(ABRecordCopyValue(people, kABPersonMiddleNamePhoneticProperty));
NSString *organization=(__bridge NSString*)(ABRecordCopyValue(people, kABPersonOrganizationProperty));
NSString *job=(__bridge NSString*)(ABRecordCopyValue(people, kABPersonJobTitleProperty));
NSString *department=(__bridge NSString*)(ABRecordCopyValue(people, kABPersonDepartmentProperty));
NSString *birthday=(__bridge NSDate*)(ABRecordCopyValue(people, kABPersonBirthdayProperty));
NSString *notes=(__bridge NSString*)(ABRecordCopyValue(people, kABPersonNoteProperty));
NSDate *creatTime=(__bridge NSDate*)(ABRecordCopyValue(people, kABPersonCreationDateProperty));
NSDate *alterTime=(__bridge NSDate*)(ABRecordCopyValue(people, kABPersonModificationDateProperty));
ABMultiValueRef address = ABRecordCopyValue(people, kABPersonAddressProperty);
for
(
int
j=0; j<ABMultiValueGetCount(address); j++) {
NSString *type = (__bridge NSString *)(ABMultiValueCopyLabelAtIndex(address, j));
NSDictionary * tempDic = (__bridge NSDictionary *)(ABMultiValueCopyValueAtIndex(address, j));
NSString *adress = [NSString stringWithFormat:@
"国家:%@\n省:%@\n市:%@\n街道:%@\n邮编:%@"
,[temDic valueForKey:(NSString*)kABPersonAddressCountryKey],[tempDic valueForKey:(NSString*)kABPersonAddressStateKey],[tempDic valueForKey:(NSString*)kABPersonAddressCityKey],[tempDic valueForKey:(NSString*)kABPersonAddressStreetKey],[tempDic valueForKey:(NSString*)kABPersonAddressZIPKey]];
}
NSData *userImage=(__bridge NSData*)(ABPersonCopyImageData(people));
NSMutableArray *dateArr = [[NSMutableArray alloc]init];
ABMultiValueRef dates= ABRecordCopyValue(people, kABPersonDateProperty);
for
(NSInteger j=0; j<ABMultiValueGetCount(dates); j++) {
NSDate *data =(__bridge NSDate*)(ABMultiValueCopyValueAtIndex(dates, j));
NSString *str =(__bridge NSString*)(ABMultiValueCopyLabelAtIndex(dates, j));
NSDictionary *tempDic = [NSDictionary dictionaryWithObject:data forKey:str];
[dateArr addObject:tempDic];
}
}
}
@end