获取设备通讯录信息

直接上代码:

#import "ViewController.h"

#import <Contacts/Contacts.h>

#import <ContactsUI/ContactsUI.h>

@interface ViewController ()<CNContactPickerDelegate>

@property (nonatomic, strong) UIButton *btn;

@property (nonatomic, strong) UILabel *label;

@property (nonatomic, strong) UILabel *labelNum;

@end

@implementation ViewController

- (void)viewDidLoad {

[super viewDidLoad];

_btn = [UIButton buttonWithType:UIButtonTypeSystem];

_btn.frame = CGRectMake(10, 20, 50, 30);

[_btn setTitle:@"按钮" forState:0];

[_btn addTarget:self action:@selector(btnAction) forControlEvents:UIControlEventTouchUpInside];

[self.view addSubview:_btn];

_label = [[UILabel alloc] initWithFrame:CGRectMake(10, 60, 50, 30)];

[self.view addSubview:_label];

_labelNum = [[UILabel alloc] initWithFrame:CGRectMake(10, 120, 50, 30)];

[self.view addSubview:_labelNum];

}

- (void)btnAction {

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;

_label.text = familyName;

for (CNLabeledValue<CNPhoneNumber*>*phone in phoneNumbers) {

NSString *label = phone.label;

CNPhoneNumber *phonNumber = (CNPhoneNumber *)phone.value;

NSLog(@"label=%@, value=%@", label, phonNumber.stringValue);

_labelNum.text = phonNumber.stringValue;

}

}

// 注意:如果实现该方法,上面那个方法就不能实现了,这两个方法只能实现一个

// - (void)contactPicker:(CNContactPickerViewController *)picker didSelectContactProperty:(CNContactProperty *)contactProperty {

//  NSLog(@"选中某个联系人的某个属性时调用");

// }

- (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;

}];

}

原文地址:https://www.cnblogs.com/xuzb/p/8677785.html

时间: 2024-10-07 16:49:49

获取设备通讯录信息的相关文章

Android上获取设备的信息

http://www.cnblogs.com/chenyingzhong/archive/2011/07/26/2117259.html Android上获取设备的信息,布布扣,bubuko.com

获取设备的信息

NSLog(@"globallyUniqueString=%@",[[NSProcessInfo processInfo] globallyUniqueString]);//全球唯一标识 NSLog(@"uniqueIdentifie=%@",[UIDevice currentDevice].uniqueIdentifier);//唯一的标识 可用于区分设备 NSLog(@"name=%@",[UIDevice currentDevice].na

安卓开发:获取手机通讯录信息

写一个安卓软件,实现获取通讯录里的人名和对应的电话号码,并且通过ListView显示出来. 因为要获取手机本地的信息,所以第一个步骤就是先给定权限咯 <uses-permission android:name="android.permission.READ_CONTACTS"/> 因为获取到信息后是通过ListView显示出来,所以把布局写好,总共两个布局,一个布局放ListView,一个布局放ListView的子布局,这里比较基础,就不放代码了 接着就是通过java代码

iOS开发-获取设备型号信息

开发中有的时候查看设计统计数据,或者通过日志查看错误信息,这个时候我们就需要获取获取设备信息,看下关于设备有几种方法: NSLog(@"%@",[[UIDevice currentDevice] name]);//设备名称 NSLog(@"%@",[[UIDevice currentDevice] model]);//设备类型 NSLog(@"%@",[[UIDevice currentDevice] localizedModel]); NSLo

android获取设备全部信息

private static final String FILE_MEMORY = "/proc/meminfo"; private static final String FILE_CPU = "/proc/cpuinfo"; /** * 得到IMEI * * @return */ public static final String getIMEI(Context context) { TelephonyManager tm = (TelephonyManage

获取ios通讯录信息

包含相应地库之后直接调用 #import <AddressBook/AddressBook.h> #import <AddressBookUI/AddressBookUI.h> -(void)readAllPeoples { //定义通讯录名字为addressbook ABAddressBookRef tmpAddressBook = nil; //根据系统版本不同,调用不同方法获取通讯录 if ([[UIDevice currentDevice].systemVersion fl

iOS读取通讯录获取好友通讯录信息[名字(姓+名字),手机号码(多个号码)等]

一.创建通讯录对象 self.addressBook=ABAddressBookCreateWithOptions(NULL, NULL); //请求访问用户通讯录,注意无论成功与否block都会调用 ABAddressBookRequestAccessWithCompletion(self.addressBook, ^(bool granted, CFErrorRef error) { if (!granted) { NSLog(@"未获得通讯录访问权限!"); } }); ABPe

iOS-JNAddressBook 获取手机通讯录信息

因为最近项目需要,要获取手机通讯并显示出来,上网搜了一下代码,重新整理了一下. 放上github:https://github.com/neng18/JNAddressBook #import <Foundation/Foundation.h> @interface JNAddressBook : NSObject @property (nonatomic,assign) int recordID; @property (nonatomic,copy) NSString *name; @pro

iOS获取设备版本信息

系统提供的接口[[UIDevice currentDevice] model]只能获取iphone,ipad无法具体到iphone6等, 下面这个接口可以获取到具体的,后续有新的系统在增加 + (NSString*)deviceVersion { // 需要加入#import "sys/utsname.h" struct utsname systemInfo; uname(&systemInfo); NSString *deviceString = [NSString stri