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

标签 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
)

现在我想区分数组中的电话号码,如家庭、手机、传真、寻呼机等。非常感谢任何帮助。

最佳答案

CNLabeledValue label 属性将返回一个代表家庭、移动等的字符串。然后调用 [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];
    }

关于ios - 从本地联系人获取详细信息后如何区分电话号码,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41607241/

相关文章:

objective-c - UIApplication openURL : doesn't work with relative URLs

iphone - PhoneGap 2.7在状态栏中显示事件指示器

ios - 如何在 swift 3 中将 UIImage 数组转换为 base64 数组?

objective-c - 托管对象上下文仅保存托管对象循环的最后一条记录

ios - 我没有在 WKWebview 中获得网页一个按钮的点击事件,如何使用 java 脚本将点击事件添加到 wkwebview

iphone - 手势识别器由于某种原因无法在 iPhone 上工作

iphone - 如何在iPhone中通过时间间隔使用coregraphics快速绘制线条

ios - iPhone : Using localization change language of an app set it in NSUserDefaults

ios - UICollectionView 使用 SDWebImage 滚动

iphone - 触摸一个文件 - 更新它的修改时间戳