我是iOS的新手,我在我的应用中提取了联系人,但是如何获取联系人ID ...假设有两个号码保存了相同的名称,那么如何获取该特定的联系人姓名' s id?
答案 0 :(得分:0)
您可以尝试这样: 导入框架
#import <Contacts/Contacts.h>
代码
- (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);
}
}
}
}];
}