简体   繁体   English

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

[英]How to differentiate the phone numbers after fetching the details from local contact

I am fetching the mobile contacts by using below method 我正在使用以下方法获取移动通讯录

-(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];
                });
            }
        }
    }];
}

I am able to get all phone numbers associated with the contact and able to store in an array. 我能够获取与联系人关联的所有电话号码,并能够存储在数组中。

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

Now i want to differentiate the phonenumbers in array like home,mobile,fax,pager etc. Any help will be really appreciated. 现在我想区分阵列中的电话号码,例如家庭,移动电话,传真,寻呼机等。任何帮助将不胜感激。

The CNLabeledValue label property will return a string that represents home, mobile etc. Then call [CNLabeledValue localizedStringForLabel:labelString] to get a localized human readable string. 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];
    }

} }

Check this blog https://genericswift.wordpress.com/ , It will fetch all contacts from contact store and will differentiate it with home,mobile,fax,pager etc. 检查此博客https://genericswift.wordpress.com/ ,它将从联系人存储中获取所有联系人,并将其与家庭,移动,传真,传呼机等区分开。

Code is in this Github link https://github.com/VinupriyaArivazhagan/AVContactPickerController 代码在此Github链接中https://github.com/VinupriyaArivazhagan/AVContactPickerController

Just need a line of code to pesent view controller with all fetched contacts 只需一行代码即可将所有获取的联系人传递到视图控制器

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

and can update design as well 并可以更新设计

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)
        })

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 获取\\ U00a,同时从联系人iOS获取多个电话号码 - Getting \U00a , While fetching multiple phone numbers from the contact iOS 如何在iPhone中识别本地电话联系人和SYNC电话联系人? - How to identify a local phone contact and an SYNC phone contact in iphone? 如果号码格式不同,如何匹配用户联系人列表中的电话号码? - How to match phone numbers from user's contact list if numbers are in different format? IOS AddressBook未从联系人列表中获取所有电话号码 - IOS AddressBook is not fetching all the phone number from contact list 是否可以从电话簿中检索我自己的联系方式? - Is it posible to retrieve my own contact details from phone book? 当没有分配给联系人的电话时,获取联系人不会崩溃 - Fetching contact no crashes when there is no phone no assigned to the contact 在SWIFT中从iPhone中获取联系电话时,应用崩溃 - App crashing while fetching contact numbers from iPhone in SWIFT 我该如何区分通话后重新启动的应用程序和从跳板启动的应用程序? - How can I differentiate between an app relaunched after a phone call vs. an app launched from springboard? 如何防止用户在文本输入中输入联系信息(电子邮件/电话号码)? - How to prevent users from entering contact information (emails/phone numbers) in text inputs? 如何区分CardDAV联系人来源? - How differentiate CardDAV contact sources?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM