#import "AppDelegate.h"
#import <AddressBook/AddressBook.h>
@interface AppDelegate ()
@end
@implementation AppDelegate
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
// Override point for customization after application launch.
[self requestAuthorizationAddressBook];
return YES;
}
- (void)requestAuthorizationAddressBook {
// 判断是否授权
ABAuthorizationStatus authorizationStatus = ABAddressBookGetAuthorizationStatus();
if (authorizationStatus == kABAuthorizationStatusNotDetermined) {
// 请求授权
ABAddressBookRef addressBookRef = ABAddressBookCreate();
ABAddressBookRequestAccessWithCompletion(addressBookRef, ^(bool granted, CFErrorRef error) {
if (granted) { // 授权成功
} else { // 授权失败
NSLog(@"授权失败!");
}
});
}
}
@end
<key>NSContactsUsageDescription</key> <string>请求访问通讯录</string>
#import "ViewController.h"
#import <AddressBook/AddressBook.h>
@interface ViewController ()
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
}
- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event {
// 1. 判读授权
ABAuthorizationStatus authorizationStatus = ABAddressBookGetAuthorizationStatus();
if (authorizationStatus != kABAuthorizationStatusAuthorized) {
NSLog(@"没有授权");
return;
}
// 2. 获取所有联系人
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
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
<key>NSContactsUsageDescription</key> <string>请求访问通讯录</string>
#import "AppDelegate.h"
#import <RHAddressBook/RHAddressBook.h>
@interface AppDelegate ()
@end
@implementation AppDelegate
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
// Override point for customization after application launch.
[self requestAuthorizationForAddressBook];
return YES;
}
- (void)requestAuthorizationForAddressBook {
RHAddressBook *ab = [[RHAddressBook alloc] init];
if ([RHAddressBook authorizationStatus] == RHAuthorizationStatusNotDetermined){
[ab requestAuthorizationWithCompletion:^(bool granted, NSError *error) {
if (granted) {
} else {
NSLog(@"请求授权拒绝");
}
}];
}
}
@end
#import "ViewController.h"
#import <RHAddressBook/RHAddressBook.h>
#import <RHAddressBook/AddressBook.h>
@interface ViewController ()
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
}
- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event {
RHAddressBook *addressBook = [[RHAddressBook alloc] init];
if ([RHAddressBook authorizationStatus] != RHAuthorizationStatusAuthorized){
NSLog(@"没有授权");
return;
}
NSArray *peopleArray= addressBook.people;
for (int i = 0; i < peopleArray.count; i++) {
RHPerson *people = (RHPerson *)peopleArray[i];
NSLog(@"%@", people.name);
RHMultiStringValue *phoneNumbers = people.phoneNumbers;
for (int i = 0; i < phoneNumbers.count; i++) {
NSString* label= [phoneNumbers labelAtIndex:i];
NSString* value= [phoneNumbers valueAtIndex:i];
NSLog(@"label=%@, value=%@", label, value);
}
NSLog(@"----------------------------------------------");
}
}
@end
#import "ViewController.h"
#import <ContactsUI/ContactsUI.h>
@interface ViewController () <CNContactPickerDelegate>
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
CNContactPickerViewController *contactPickerViewController = [[CNContactPickerViewController alloc] init];
contactPickerViewController.delegate = self;
[self presentViewController:contactPickerViewController animated:YES completion:nil];
}
// 如果实现该方法当选中联系人时就不会再出现联系人详情界面, 如果需要看到联系人详情界面只能不实现这个方法,
- (void)contactPicker:(CNContactPickerViewController *)picker didSelectContact:(CNContact *)contact {
NSLog(@"选中某一个联系人时调用---------------------------------");
[self printContactInfo:contact];
}
// 同时选中多个联系人
- (void)contactPicker:(CNContactPickerViewController *)picker didSelectContacts:(NSArray<CNContact *> *)contacts {
for (CNContact *contact in contacts) {
NSLog(@"================================================");
[self printContactInfo:contact];
}
}
- (void)printContactInfo:(CNContact *)contact {
NSString *givenName = contact.givenName;
NSString *familyName = contact.familyName;
NSLog(@"givenName=%@, familyName=%@", givenName, familyName);
NSArray * phoneNumbers = contact.phoneNumbers;
for (CNLabeledValue<CNPhoneNumber*>*phone in phoneNumbers) {
NSString *label = phone.label;
CNPhoneNumber *phonNumber = (CNPhoneNumber *)phone.value;
NSLog(@"label=%@, value=%@", label, phonNumber.stringValue);
}
}
// 注意:如果实现该方法,上面那个方法就不能实现了,这两个方法只能实现一个
//- (void)contactPicker:(CNContactPickerViewController *)picker didSelectContactProperty:(CNContactProperty *)contactProperty {
// NSLog(@"选中某个联系人的某个属性时调用");
//}
@end
<key>NSContactsUsageDescription</key> <string>请求访问通讯录</string>
#import "AppDelegate.h"
#import <Contacts/Contacts.h>
@interface AppDelegate ()
@end
@implementation AppDelegate
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
// Override point for customization after application launch.
[self requestAuthorizationForAddressBook];
return YES;
}
- (void)requestAuthorizationForAddressBook {
CNAuthorizationStatus authorizationStatus = [CNContactStore authorizationStatusForEntityType:CNEntityTypeContacts];
if (authorizationStatus == CNAuthorizationStatusNotDetermined) {
CNContactStore *contactStore = [[CNContactStore alloc] init];
[contactStore requestAccessForEntityType:CNEntityTypeContacts completionHandler:^(BOOL granted, NSError * _Nullable error) {
if (granted) {
} else {
NSLog(@"授权失败, error=%@", error);
}
}];
}
}
@end
#import "ViewController.h"
#import <Contacts/Contacts.h>
@interface ViewController ()
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
}
- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event {
CNAuthorizationStatus authorizationStatus = [CNContactStore authorizationStatusForEntityType:CNEntityTypeContacts];
if (authorizationStatus == CNAuthorizationStatusAuthorized) {
NSLog(@"没有授权...");
}
// 获取指定的字段,并不是要获取所有字段,需要指定具体的字段
NSArray *keysToFetch = @[CNContactGivenNameKey, CNContactFamilyNameKey, CNContactPhoneNumbersKey];
CNContactFetchRequest *fetchRequest = [[CNContactFetchRequest alloc] initWithKeysToFetch:keysToFetch];
CNContactStore *contactStore = [[CNContactStore alloc] init];
[contactStore enumerateContactsWithFetchRequest:fetchRequest error:nil usingBlock:^(CNContact * _Nonnull contact, BOOL * _Nonnull stop) {
NSLog(@"-------------------------------------------------------");
NSString *givenName = contact.givenName;
NSString *familyName = contact.familyName;
NSLog(@"givenName=%@, familyName=%@", givenName, familyName);
NSArray *phoneNumbers = contact.phoneNumbers;
for (CNLabeledValue *labelValue in phoneNumbers) {
NSString *label = labelValue.label;
CNPhoneNumber *phoneNumber = labelValue.value;
NSLog(@"label=%@, phone=%@", label, phoneNumber.stringValue);
}
// *stop = YES; // 停止循环,相当于break;
}];
}
@end
机械节能产品生产企业官网模板...
大气智能家居家具装修装饰类企业通用网站模板...
礼品公司网站模板
宽屏简约大气婚纱摄影影楼模板...
蓝白WAP手机综合医院类整站源码(独立后台)...苏ICP备2024110244号-2 苏公网安备32050702011978号 增值电信业务经营许可证编号:苏B2-20251499 | Copyright 2018 - 2025 源码网商城 (www.ymwmall.com) 版权所有