ios 简书 获取通讯录信息_iOS 关于iPhone通讯录信息获取

/**

* keys: @[CNContactPhoneNumbersKey, 手机号

* CNContactFamilyNameKey, 姓氏

* CNContactGivenNameKey 名字

* ...,nil ]; @see 内有详情。

* 选择需要的信息。

*

* contacts:返回NSArray。

* flag:0=成功,-1=用户拒绝,-2=需在设置界面开启通讯录,-3=参数有误;

*/

+(void)scanContactsWithKeys:(NSArray*)keys

Result:(void(^)(NSArray*contacts,

int flag))result;

/**

* 简单解析 contacts

* {@"FamilyName":@"...",

* @"GivenName":@"...",

* @"Phones":@[@"iphone":@"110",@"home":@"456",...]}

*/

+(NSDictionary*)openContact:(CNContact*)contact;

//实现代码:

static CNContactStore *store;

+(void)scanContactsWithKeys:(NSArray*)keys

Result:(void(^)(NSArray*contacts,

int flag))result

{

if (keys.count == 0) {

NSLog(@"Error: DFContacts keys is nil.");

if (result) result(nil,-3);

return;

}

NSMutableArray *mArr = [NSMutableArray new];

CNContactFetchRequest *request = [[CNContactFetchRequest alloc] initWithKeysToFetch:keys];

CNAuthorizationStatus status = [CNContactStore authorizationStatusForEntityType:CNEntityTypeContacts];

if (status == CNAuthorizationStatusNotDetermined) {

NSLog(@"DFContacts is applying for authorization...");

store = [CNContactStore new];

[store requestAccessForEntityType:CNEntityTypeContacts

completionHandler:^(BOOL granted,

NSError * _Nullable error)

{

if (granted){

// 获取通讯录中所有的联系人

[store enumerateContactsWithFetchRequest:request error:nil

usingBlock:^(CNContact * _Nonnull contact,

BOOL * _Nonnull stop)

{

[mArr addObject:contact];

}];

if (result) result(mArr,0);

}else{

NSLog(@"Error: User refusal to authorize.");

if (result) result(nil,-1);

}

}];

}

if (status == CNAuthorizationStatusRestricted) {

NSLog(@"Error: You need to open the address book at the settings interface.");

if (result) result(nil,-2);

}

if (status == CNAuthorizationStatusDenied) {

NSLog(@"Error: User refusal to authorize.");

if (result) result(nil,-1);

}

if (status == CNAuthorizationStatusAuthorized) {

store = [CNContactStore new];

[store enumerateContactsWithFetchRequest:request

error:nil

usingBlock:^(CNContact * _Nonnull contact,

BOOL * _Nonnull stop)

{

[mArr addObject:contact];

}];

if (result) result(mArr,0);

}

}

+(NSDictionary*)openContact:(CNContact*)contact{

NSString *familyName = contact.familyName; // 姓氏

NSString *givenName = contact.givenName; // 名字

// 获取电话号码

NSMutableArray *mPhones = [NSMutableArray new];

for (CNLabeledValue *labeledValue in contact.phoneNumbers)

{

CNPhoneNumber *phoneValue = labeledValue.value;

NSString *phoneNumber = phoneValue.stringValue;

NSString *label = [CNLabeledValue localizedStringForLabel:labeledValue.label];

NSLog(@"%@--%@",label,phoneNumber);

NSDictionary *phoneDic =@{label?:@"unknow":phoneNumber?:@""};

[mPhones addObject:phoneDic];

}

NSDictionary *info = @{@"FamilyName":familyName,@"GivenName":givenName,@"Phones":mPhones};

return info;

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值