【问题标题】:How to check contact source in CNContact swift?如何在 CNContact swift 中检查联系人来源?
【发布时间】:2016-12-14 00:40:13
【问题描述】:

在联系人应用程序中有“iCloud”、“yahoo”、“gmail”等组。在swift中,是否可以仅从gmail源获取联系人?

【问题讨论】:

    标签: ios swift google-contacts-api swift3 cncontact


    【解决方案1】:

    经过测试的代码。希望它能解决你的问题...

       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
        }
    

    【讨论】:

    • “通讯录”真的是这样吗?谁为该组获取 gmail
    • 我不会说 Yahoo 或 Facebook 可以安全地在此处声明,因为 Outlook.com 容器也使用“联系人”作为名称,其他任何内容都会在此代码中显示 Facebook跨度>
    【解决方案2】:

    iCloud/yahoo/gmail 等都是 CNContainer。 Gmail/iCloud 的类型为 CNContainerTypeCardDAV。因此,首先您需要获取所有联系人,然后根据该联系人的 CNContainerType 过滤数组。但不幸的是,我们无法识别它是哪个 CardDav,即 iCloud/Gmail。

    请在此处查看更多详细信息:How do we know which CNContainer represents iCloud?

    【讨论】:

      【解决方案3】:

      您可以通过查看此处的联系人框架运行时标头来实现此目的: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,但应该也可以。

      对不起我的英语。 祝你好运:)

      【讨论】:

      • 执行选择器对我不起作用并且它会崩溃。有没有办法做到这一点?
      • @Sunil_Vaishnav 您能否提供导致崩溃的代码?还是你用的是我写的?
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-01-30
      相关资源
      最近更新 更多