【发布时间】:2016-12-14 00:40:13
【问题描述】:
在联系人应用程序中有“iCloud”、“yahoo”、“gmail”等组。在swift中,是否可以仅从gmail源获取联系人?
【问题讨论】:
标签: ios swift google-contacts-api swift3 cncontact
在联系人应用程序中有“iCloud”、“yahoo”、“gmail”等组。在swift中,是否可以仅从gmail源获取联系人?
【问题讨论】:
标签: ios swift google-contacts-api swift3 cncontact
经过测试的代码。希望它能解决你的问题...
func getAppropriateName(for container: CNContainer?) -> String? {
var name = ""
if (container?.name == "Card") || container?.name == nil {
name = "iCloud"
}
else if (container?.name == "Address Book") {
name = "Google"
}
else if (container?.name == "Contacts") {
name = "Yahoo"
}
else {
name = "Facebook"
}
return name
}
【讨论】:
iCloud/yahoo/gmail 等都是 CNContainer。 Gmail/iCloud 的类型为 CNContainerTypeCardDAV。因此,首先您需要获取所有联系人,然后根据该联系人的 CNContainerType 过滤数组。但不幸的是,我们无法识别它是哪个 CardDav,即 iCloud/Gmail。
请在此处查看更多详细信息:How do we know which CNContainer represents iCloud?
【讨论】:
您可以通过查看此处的联系人框架运行时标头来实现此目的:https://github.com/JaviSoto/iOS10-Runtime-Headers/tree/master/Frameworks/Contacts.framework
您可以通过performSelector 消息给他们打电话。有点乱,但是很管用。
通常你需要做的是:
CNContactStore* store = [CNContactStore new];
// fetch accounts that sync contacts with your device (array of CNAccount)
// since CNAccount class isn't available by default, we treat it as NSObject for our puproses
NSArray* accounts = [store performSelector:@selector(accountsMatchingPredicate:error:) withObject:nil withObject:nil];
// you can iterate through this array, I just use first one for this example
NSObject* account = [accounts firstObject];
// get identifier of the account for NSPredicate we use next
NSString* accountId = [account performSelector:@selector(identifier)];
// Display name of the account (aka Yahoo, Gmail etc.)
NSString* accountName = [account performSelector:@selector(_cnui_displayName)];
// NSPredicate that help us to get corresponding CNContainer
NSPredicate* containerPredicate = [[CNContainer class] performSelector:@selector(predicateForContainersInAccountWithIdentifier:) withObject:accountId];
// Fetching CNContainer
CNContainer* container = [[store containersMatchingPredicate:containerPredicate error:nil] firstObject];
接下来就是 CNContainers 的一般用法了。 希望它会有所帮助。
附言。它适用于 iOS 10,对于未来的版本,您应该检查 Contacts.framework 运行时更改。
PPS。我没有检查 swift,但应该也可以。
对不起我的英语。 祝你好运:)
【讨论】: