从本地联系人处获取详细信息后如何区分电话号码

时间:2017-01-12 07:18:52

标签: ios objective-c iphone xcode cncontact

我使用以下方法

获取移动联系人
-(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
)

现在我想区分家庭,移动,传真,寻呼机等阵列中的电话号码。任何帮助都会非常感激。

2 个答案:

答案 0 :(得分:1)

CNLabeledValue label属性将返回表示home,mobile等的字符串。然后调用[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];
    }

}

答案 1 :(得分:0)

查看此博客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)
        })