ios - How to Determine if a Contact is Stored in iCloud, Google, or Another Account in Swift? - Stack Overflow

I am using CNContactStore to fetch contacts from an iOS device. While retrieving contacts, I want to ch

I am using CNContactStore to fetch contacts from an iOS device. While retrieving contacts, I want to check whether each contact is stored in iCloud, Google, Yahoo, or another account. However, I am only getting generic container names like "Card" or "Address Book" instead of the actual account names.

Here is my current code:

func fetchContacts(completion: @escaping ([ContactModel]) -> Void) {
    let store = CNContactStore()
    
    DispatchQueue.global(qos: .userInitiated).async {
        let keys: [CNKeyDescriptor] = [
            CNContactIdentifierKey as CNKeyDescriptor,
            CNContactGivenNameKey as CNKeyDescriptor,
            CNContactFamilyNameKey as CNKeyDescriptor,
            CNContactPhoneNumbersKey as CNKeyDescriptor
        ]
        
        var contacts = [ContactModel]()
        
        do {
            // Fetch all containers (iCloud, Google, Outlook, etc.)
            let containers = try store.containers(matching: nil)
            
            for container in containers {
                let request = CNContactFetchRequest(keysToFetch: keys)
                request.predicate = CNContact.predicateForContactsInContainer(withIdentifier: container.identifier)
                
                try store.enumerateContacts(with: request) { (contact, _) in
                    let fullName = "\(contact.givenName) \(contact.familyName)"
                    let phoneNumbers = contact.phoneNumbers.map { $0.value.stringValue }
                    
                    // Checking container type
                    print("Container Name: \(container.name), Type: \(container.type.rawValue)")

                    // Attempting to map account type
                    let accountName: String
                    switch container.type {
                    case .cardDAV:
                        accountName = container.name.contains("iCloud") ? "iCloud" : "Google/Yahoo (CardDAV)"
                    case .exchange:
                        accountName = "Outlook/Exchange"
                    case .local:
                        accountName = "Device Contacts"
                    default:
                        accountName = container.name
                    }
                    
                    let contactModel = ContactModel(contact: contact, accountName: accountName)
                    contacts.append(contactModel)
                }
            }
            
            DispatchQueue.main.async {
                completion(contacts)
            }
        } catch {
            print("Failed to fetch contacts: \(error)")
        }
    }
}

Using CNContainer.name → Returns "Card" or "Address Book", not "iCloud", "Google", etc.

How can I accurately determine whether a contact belongs to iCloud, Google, Yahoo, or another account?

I am using CNContactStore to fetch contacts from an iOS device. While retrieving contacts, I want to check whether each contact is stored in iCloud, Google, Yahoo, or another account. However, I am only getting generic container names like "Card" or "Address Book" instead of the actual account names.

Here is my current code:

func fetchContacts(completion: @escaping ([ContactModel]) -> Void) {
    let store = CNContactStore()
    
    DispatchQueue.global(qos: .userInitiated).async {
        let keys: [CNKeyDescriptor] = [
            CNContactIdentifierKey as CNKeyDescriptor,
            CNContactGivenNameKey as CNKeyDescriptor,
            CNContactFamilyNameKey as CNKeyDescriptor,
            CNContactPhoneNumbersKey as CNKeyDescriptor
        ]
        
        var contacts = [ContactModel]()
        
        do {
            // Fetch all containers (iCloud, Google, Outlook, etc.)
            let containers = try store.containers(matching: nil)
            
            for container in containers {
                let request = CNContactFetchRequest(keysToFetch: keys)
                request.predicate = CNContact.predicateForContactsInContainer(withIdentifier: container.identifier)
                
                try store.enumerateContacts(with: request) { (contact, _) in
                    let fullName = "\(contact.givenName) \(contact.familyName)"
                    let phoneNumbers = contact.phoneNumbers.map { $0.value.stringValue }
                    
                    // Checking container type
                    print("Container Name: \(container.name), Type: \(container.type.rawValue)")

                    // Attempting to map account type
                    let accountName: String
                    switch container.type {
                    case .cardDAV:
                        accountName = container.name.contains("iCloud") ? "iCloud" : "Google/Yahoo (CardDAV)"
                    case .exchange:
                        accountName = "Outlook/Exchange"
                    case .local:
                        accountName = "Device Contacts"
                    default:
                        accountName = container.name
                    }
                    
                    let contactModel = ContactModel(contact: contact, accountName: accountName)
                    contacts.append(contactModel)
                }
            }
            
            DispatchQueue.main.async {
                completion(contacts)
            }
        } catch {
            print("Failed to fetch contacts: \(error)")
        }
    }
}

Using CNContainer.name → Returns "Card" or "Address Book", not "iCloud", "Google", etc.

How can I accurately determine whether a contact belongs to iCloud, Google, Yahoo, or another account?

Share Improve this question edited Feb 27 at 13:13 Kiryl Famin 33310 bronze badges asked Feb 27 at 10:26 Codebane the SwiftbreakerCodebane the Swiftbreaker 871 silver badge8 bronze badges
Add a comment  | 

2 Answers 2

Reset to default 0

As already discussed here, the Contacts framework does not provide information as to whether a particular container corresponds to iCloud, Google, Yahoo. The only additional info you can get is using CNContainer.type:

CNContainerType.cardDAV: Typically used for iCloud, Google, Yahoo contacts. However, distinguishing between Google and Yahoo requires additional logic.

CNContainerType.exchange: Used for Outlook/Exchange contacts.

CNContainerType.local: Used for "On My iPhone" contacts.

in Container object which we fetch we got this properties externalIdentifier=/carddav/v1/principals/garejakirit%40gmail/lists/default/

externalModificationTag="6bae89da88eb93cc.701"

externalSyncTag=https://www.googleapis/carddav/v1/synctoken/0802100118E4A18AF598E68B03220C08A4A886BE0610D8E2FA8701

so using externalIdentifier we can get gmail or any other email and if iCloud than a 64 letter unique code

and using this externalSyncTag we can get source if google than we got google api and if any other than that api so that's how we can determine the source of contact i did like this but is there any better way to do that please discuss here

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1741062250a4299901.html

相关推荐

  • MCP实战

    前言最近热衷于找一些好玩的MCP,集成在cursor中,给大模型外挂许多有趣的功能,例如:什么是MCP?本地如何开发MCP ServerMCP实战 | cursor 如何一句话操作 gitHub 代码库cursor 如何调用 MCP ser

    1小时前
    00
  • 方法分享

    作者,Evil Genius现在的日子,关税在涨,物价在涨,钱是越来越难挣了。什么东西在跌呢?工资在跌,房价也跌,房租反而涨了,生活的压力感觉越来越大了。对于生信分析而言,什么东西是最重要的?尤其是空间转录组的分析?很显然,硬件是服务器,软

    1小时前
    00
  • win 7系统怎么看计算机配置,windows7系统怎么查看电脑配置

    通常我们在选购电脑的时候都比较注重电脑配置,那么windows7系统怎么查看电脑配置呢?接下来大家跟着学习啦小编一起来了解一下windows7查看电脑配置的解决方法吧。 windows7查看电脑配置方法一&#x

    1小时前
    00
  • VMware虚拟机设置为固定IP

    操作原因:安装完成虚拟机后会生成一个IP,由于ip变化导致搭建好的k8s集群无法启动。因此将虚拟机配置为固定IP1、修改虚拟网络编辑器:打开虚拟网络编辑器,修改NAT设置:2、修改电脑的虚拟网卡地址选中这个网卡-->点击右键-->

    1小时前
    00
  • PDF转换Word深度评测

    ComPDFKit PDF 转换 SDK V3.0有以下几个新功能:使用百万级文档训练数据集对 PPYoloE AI 模型进行微调全场景布局分析算法及下一代表格识别算法重构数据结构、转换流程、PDF解析和输出模块混合布局:将流式布局与固定布

    1小时前
    00
  • Elasticsearch BBQ与OpenSearch FAISS:向量搜索性能对比

    基于二进制量化的向量搜索:Elasticsearch使用BBQ技术比OpenSearch搭配FAISS快5倍。我们收到社区要求,希望能解释Elasticsearch与OpenSearch在语义搜索向量搜索方面的性能差异。因此,我们进行了一

    56分钟前
    00
  • 8.6K star!完全免费+本地运行+无需GPU,这款AI搜索聚合神器绝了!

    嗨,大家好,我是小华同学,关注我们获得“最新、最全、最优质”开源项目和高效工作学习方法 FreeAskInternet是一款革命性的开源项目,它完美结合了多引擎搜索和智能语言模型,让你在不联网、不花钱、不暴露隐私的情况下,获得媲美ChatG

    50分钟前
    00
  • 苹果电脑装win7系统问题

    安装中问题: Windows 未能启动。原因可能是最近更改了硬件或软件。解决此问题的步骤: 1. 插入Windows 安装光盘并重新启动计算机。 2. 选择语言设置,然后单击

    48分钟前
    00
  • 技术好文|不用背代码!用动画征服算法!

    不用背代码!用动画征服算法!用腾讯云Cloud Studio的 AI助手,让算法“动”起来

    43分钟前
    00
  • 皮尔兹Pnoz c1c2安全继电器配置与接线

    PNOZ compact是皮尔兹的一款经济型的安全继电器系列,价格实惠,空间紧凑,适用于安装急停按钮、安全门或光障光栅 等安全产品。PNOZ C1是适用急停按钮或安全门开关。PNOZ C2用于 4 类光障或带 OSSD 输出的传感器的安全

    39分钟前
    00
  • KB2533623补丁安装指南 - 解决Win7下无法运行.NET 67 WinForm应用

    KB2533623补丁安装指南 - 解决Win7下无法运行.NET 67 WinForm应用 项目地址:https:gitcodeopen-source-toolkit62304 概览 对于所有遇到Windows 7系统无法

    35分钟前
    00
  • Sentinel 的熔断和限流

    在分布式系统里,服务之间牵一发而动全身,一个接口雪崩,可能带崩整个应用链路。要想系统抗住流量洪峰,顶住突发异常,就得在稳定性上下功夫。今天我就来说说稳定性保障里的老将——Sentinel,看看它是怎么凭借限流熔断,在服务治理的江湖里占得一席

    33分钟前
    00
  • OceanBase 单机版可以大批量快速部署吗? YES

    OceanBase的单机版已经开放给一些老师测试了,有幸被邀请进行单机测试,这次可以测试的是商业版,我已经好久没有尝试一个商业版线下的数据库试用了,上一次还是SQL SERVER 2019 的180天版本。测试企业OB版本测试企业OB版本目

    29分钟前
    00
  • MTVInpaint:多任务视频修复框架,以双分支注意力与两阶段流水线统一完成、插入任务,处理长视频 !

    视频修复涉及在视频中修改局部区域,确保空间和时间上的一致性。现有的大多数方法主要集中在场景完成(即填补缺失区域)上,并缺乏以可控方式在场景中插入新目标的能力。幸运的是,最近在文本到视频(T2V)扩散模型方面的进展为文本指导的视频修复铺平了

    28分钟前
    20
  • Oracle中删除的列数据可以进行恢复么?

    有朋友提出闪回可以恢复删除的列(包括数据),这个可行么?实践是检验真理的唯一标准,创建一张测试表,代码语言:javascript代码运行次数:0运行复制CREATE TABLE t_flash_01 (id NUMBER, c1 varch

    26分钟前
    20
  • AI|微信又又又有大动作了,直接把Deepseek变成了你的微信好友!

    腾讯又又又放大招了!在全球都在AI的浪潮中,以及年初Deepseek横空出世,使得中国的AI开始“吊打”海外的AI后,国内的AI使用也如雨后春笋般层出不穷。但是对于很多人来说AI的使用依然是一个门槛儿,手机上需要安装APP或者各种小程序跳转

    18分钟前
    20
  • MCE干货分享

    在蛋白功能研究中,亲和力测定是揭示分子互作机制的关键环节。然而,传统方法通常依赖高纯度蛋白,而许多蛋白如膜蛋白、低溶解度蛋白等的研究就受到了限制。微量热泳动技术 (MST) 以其高灵敏度、低样本需求和对粗样品的兼容性,不仅可以检测重组蛋白,

    11分钟前
    00
  • OFC 2025:IBM开发应用于CPO的高密度聚合物波导光子模块

    一、技术背景:突破电互连瓶颈在服务器硬件中,计算单元的带宽每两年以约 3 倍的速度增长,而互连技术仅以 1.4 倍的速度发展,导致 “带宽鸿沟” 持续扩大。传统可插拔收发模块虽能扩展传输距离,但在高密度集成和功耗上存在局限。

    9分钟前
    00
  • 亲测!Gemini一句话复现顶刊论文图片,碾压ChatGPTGrok 3

    论文图片复现是新手小白学习过程中的必经之路,但在实际操作中时常面临诸多挑战。随着人工智能技术的进步,我们现可借助AI模型来复现论文图片,从而更深入地理解原文逻辑和研究方法。然而,不同AI模型在论文图像复现能力上存在明显差异。通过对Gemi

    2分钟前
    00
  • Nature证实:ChatGPT能够有效辅助医生提升决策准确率

    近期,Nature Medicine 刊登了一篇关于GPT-4在临床应用的文章《GPT-4 assistance for improvement of physician performance on patient care tasks:

    55秒前
    00

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信