【问题标题】:How to differentiate the phone numbers after fetching the details from local contact从本地联系人获取详细信息后如何区分电话号码
【发布时间】:2017-01-12 07:18:51
【问题描述】:

我正在使用以下方法获取手机联系人

-(void)fetchContactsandAuthorization
{
    // Request authorization to Contacts
    CNContactStore *store = [[CNContactStore alloc] init];
    [store requestAccessForEntityType:CNEntityTypeContacts completionHandler:^(BOOL granted, NSError * _Nullable error) {
        if (granted == YES)
        {
            //make sure that you have added the necessary properties
            NSArray *keys = @[CNContactFamilyNameKey, CNContactGivenNameKey, CNContactPhoneNumbersKey, CNContactImageDataKey, CNContactPostalAddressesKey];
            NSString *containerId = store.defaultContainerIdentifier;
            NSPredicate *predicate = [CNContact predicateForContactsInContainerWithIdentifier:containerId];
            NSError *error;
            NSArray *cnContacts = [store unifiedContactsMatchingPredicate:predicate keysToFetch:keys error:&error];

            NSLog(@"new %@",cnContacts);

            if (error)
            {
                NSLog(@"error fetching contacts %@", error);
            }
            else
            {
                NSString *phone;
                NSString *fullName;
                NSString *firstName;
                NSString *lastName;
                NSString *companyName;
                NSString *departmentName;
                NSString *jobTitleName;
                NSString *address;
                NSString *iden;
                NSString *emailAddress;

                UIImage *profileImage;

                NSMutableArray *contactNumbersArray = [[NSMutableArray alloc]init];
                NSMutableArray *addressArray = [[NSMutableArray alloc]init];
                NSMutableArray *emailAddressArray = [[NSMutableArray alloc]init];
                for (CNContact *contact in cnContacts) {
                    // copy data to my custom Contacts class.
                    firstName = contact.givenName;
                    lastName = contact.familyName;
                    iden = contact.identifier;


                    if (lastName == nil) {
                        fullName=[NSString stringWithFormat:@"%@",firstName];
                    }else if (firstName == nil){
                        fullName=[NSString stringWithFormat:@"%@",lastName];
                    }
                    else{
                        fullName=[NSString stringWithFormat:@"%@ %@",firstName,lastName];
                    }
                    UIImage *image = [UIImage imageWithData:contact.imageData];

                    NSLog(@"imgold %@",image);
                    if (image != nil) {
                        profileImage = image;
                    }else{
                        profileImage = [UIImage imageNamed:@"person-icon.png"];
                    }
                    for (CNLabeledValue *label in contact.phoneNumbers) {
                        phone = [label.value stringValue];
                        if ([phone length] > 0) {
                            [contactNumbersArray addObject:phone];
                        }
                    }
                    NSLog(@"PhonenumberArray %@",contactNumbersArray);


                    User *user = [User new];
                    user.fullName=fullName;
                    user.image= profileImage;
                    user.phone= phone;
                    user.idUser= iden;
                    [contacts addObject:user];

                }
                dispatch_async(dispatch_get_main_queue(), ^{
                    [_selectContactListTblView reloadData];
                });
            }
        }
    }];
}

我能够获取与联系人关联的所有电话号码并能够存储在一个数组中。

PhonenumberArray (
    "98708\U00a001224",
    "98920\U00a077702",
    "93240\U00a077702",
    1,
    2,
    3,
    4,
    5,
    5,
    6,
    7,
    8,
    9
)

现在我想区分家庭、手机、传真、寻呼机等数组中的电话号码。任何帮助将不胜感激。

【问题讨论】:

  • @HimanshuMoradiya,检查您的链接。它显示了如何获取我已经能够做到的电话号码。我的问题是识别电话号码标签,如家庭或手机等。

标签: ios objective-c iphone xcode cncontact


【解决方案1】:

CNLabeledValue 标签属性将返回一个代表家庭、移动设备等的字符串。然后调用[CNLabeledValue localizedStringForLabel:labelString] 以获取本地化的人类可读字符串。

for (CNLabeledValue *label in contact.phoneNumbers) {
    phone = [label.value stringValue];
    labelString = label.label;
    if ([phone length] > 0) {
         [contactNumbersArray addObject:phone];
         [contactNumbersLabelsArray addObject:labelString];
    }

}

【讨论】:

  • 如果可能的话,您可以发布一些示例代码来调用 [CNLabeledValuelocalizedStringForLabel:labelString]
【解决方案2】:

查看此博客https://genericswift.wordpress.com/,它将从联系人存储中获取所有联系人,并将其与家庭、手机、传真、寻呼机等区分开来。

代码在这个 Github 链接https://github.com/VinupriyaArivazhagan/AVContactPickerController

只需要一行代码来显示所有获取的联系人的视图控制器

AVContactPickerController.present(title: "Contact", maximumContactCount: nil, updateDesign: nil)

还可以更新设计

AVContactPickerController.present(title: "Contact", maximumContactCount: 2, updateDesign: { controller in

            controller.checkImage = #imageLiteral(resourceName: "Clicked")
            controller.uncheckImage = #imageLiteral(resourceName: "Click")
            controller.closeButton.setTitleColor(UIColor.red, for: .normal)
        })

【讨论】:

  • 这很快。我的代码是客观的 c。请发布与目标 c 相关的任何参考。
  • @Madhu,您可以从目标 c 调用 swift 类函数,将该 swift 文件添加到您的项目文件夹中,执行必要的步骤(只需 google 即可获取额外信息),然后您可以调用 swift 类文件函数来自目标 c 类文件
  • 查看该博客以了解如何使用该 swift 文件
  • 感谢您的回复。我需要在目标 c 中构建它。我不能在我的项目中使用 swift 文件。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-02-25
  • 1970-01-01
  • 2012-02-21
  • 1970-01-01
相关资源
最近更新 更多