简体   繁体   中英

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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