【问题标题】:How To Get Account Name From Contact Framework如何从联系人框架中获取帐户名称
【发布时间】:2017-05-23 11:33:52
【问题描述】:
我们知道 iOS 中的联系人可以从Google、iCloud 和Phone 同步。好吧,我们可以使用Contacts.framework 获取一堆联系人,但我想知道它属于哪个帐户。
我的意思是,我需要区分电子邮件和电话同步的联系人。有什么办法吗?
我正在使用contact framework。我通过使用CNContainer 获取标识符,但是如何获取存储联系人的帐户名称以及如何从该帐户中获取该联系人?
【问题讨论】:
标签:
ios
iphone
swift
ios10.3
【解决方案1】:
您能否尝试获取所有容器,然后您可以根据容器名称对这些联系人进行分组
-(void)fetchContacts
{
CNContactStore *store = [[CNContactStore alloc] init];
[store requestAccessForEntityType:CNEntityTypeContacts completionHandler:^(BOOL granted, NSError * _Nullable error) {
if (granted == YES)
{
NSArray * contactContainerArray = [store containersMatchingPredicate:nil error:nil];
for(CNContainer * container in contactContainerArray) {
NSLog(@"Container name == %@ ",container.name);
NSPredicate *predicate = [CNContact predicateForContactsInContainerWithIdentifier:container.identifier];
NSError *error;
NSArray *cnContacts = [store unifiedContactsMatchingPredicate:predicate keysToFetch:keys error:&error];
if (error)
{
NSLog(@"error fetching contacts %@", error);
}
else
{
for (CNContact *contact in cnContacts) {
NSMutableArray *contactNumbersArray = [[NSMutableArray alloc]init];
// contact.givenName;
// contact.familyName;
for (CNLabeledValue *label in contact.phoneNumbers) {
// [label.value stringValue]; Phone Number
}
}
}
}
}
}];
}