iOS 关于iPhone通讯录信息获取

注意事项:

  • 添加头文件 【#import
  • 在Xcode添加【info.plist 的Privacy - Contacts Usage Description
  • 若APP功能没必要用到通讯录,但代码上却又添加了,上架会被拒绝,值得注意。

代码如下:

/**
 *  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;
}

你可能感兴趣的:(iOS 关于iPhone通讯录信息获取)