我已经使用 cncontacts 获取联系人并能够在控制台屏幕上显示它们,但不能在 table 视图中显示
I have fetched the contact using cncontacts and able to display them on console screen, but not on table view
如何在table视图中显示联系人?
我已经通过 CNContactStore 获取了联系人 iOS 9.
- (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 *cnContactsarray = [store unifiedContactsMatchingPredicate:predicate keysToFetch:keys error:&error];
if (error) {
NSLog(@"error fetching contacts %@", error);
}
else {
for (CNContact *contact in cnContactsarray) {
//store all the contacts as per your requirement
NSLog(@"Name : %@",contact.givenName);
NSLog(@"Id : %@",contact.identifier);//the contact id which you want
[_ContactsCN addObject:contact];
}
}
NSLog(@"array %@",_ContactsCN);
}
}];
}
任何帮助将不胜感激。
当您从 CNContact 获取联系人完成后,您需要在主线程上重新加载数据
到 运行 在主线程上
dispatch_async(dispatch_get_main_queue(), ^{// reload your table here
});
如何在table视图中显示联系人?
我已经通过 CNContactStore 获取了联系人 iOS 9.
- (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 *cnContactsarray = [store unifiedContactsMatchingPredicate:predicate keysToFetch:keys error:&error];
if (error) {
NSLog(@"error fetching contacts %@", error);
}
else {
for (CNContact *contact in cnContactsarray) {
//store all the contacts as per your requirement
NSLog(@"Name : %@",contact.givenName);
NSLog(@"Id : %@",contact.identifier);//the contact id which you want
[_ContactsCN addObject:contact];
}
}
NSLog(@"array %@",_ContactsCN);
}
}];
}
任何帮助将不胜感激。
当您从 CNContact 获取联系人完成后,您需要在主线程上重新加载数据
到 运行 在主线程上
dispatch_async(dispatch_get_main_queue(), ^{// reload your table here
});