简体   繁体   English

如何在iOS应用中获取联系人ID?

[英]How to fetch contact id in your iOS app?

我是iOS的新手,已经在我的应用程序中获取了联系人,但是如何获取联系人ID ...假设是否有两个用相同名称保存的号码,那么如何获取该特定联系人姓名的ID?

You may try like this : import framework 您可以这样尝试:导入框架

#import <Contacts/Contacts.h>

Code

- (void) getContacts {
    CNContactStore *store = [[CNContactStore alloc] init];
    [store requestAccessForEntityType:CNEntityTypeContacts completionHandler:^(BOOL granted, NSError * _Nullable error) {
        if (granted == YES) {
            //keys with fetching properties
            NSArray *keys = @[CNContactFamilyNameKey, CNContactGivenNameKey, CNContactPhoneNumbersKey, CNContactImageDataKey];
            NSString *containerId = store.defaultContainerIdentifier;
            NSPredicate *predicate = [CNContact predicateForContactsInContainerWithIdentifier:containerId];
            NSError *error;
            NSArray *cnContacts = [store unifiedContactsMatchingPredicate:predicate keysToFetch:keys error:&error];
            if (error) {
                NSLog(@"error fetching contacts %@", error);
            } else {
                for (CNContact *contact in cnContacts) {
                    //store all the contacts as per your requirement
                    NSLog(@"Id %@",contact.identifier);//the contact id which you want
                    NSLog(@"Name %@",contact.givenName);
                }
            }
        }        
    }];
}

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM