本文介绍了如何强制将新的CNContact放入本地CNContainer?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在编写一个使用CNContactStore将通过REST和JSON获取的联系人信息存储到容器中的应用程序.我想将这些联系人与任何其他帐户分开,仅存储在设备上的本地帐户中,但是不存在本地商店,并且找不到任何创建/激活帐户的方法?

I'm writing an app that stores contact-info fetched through REST and JSON into a container, using CNContactStore. I would like to keep these contacts separate from any other accounts, and only stored locally on the device, but a local store doesn't exist, and I can't find any way to create/activate one?

我可以使用以下方法获取默认商店的ID(例如在设备上配置的iCloud):

I'm able to get the default store's ID (as configured on the device, e.g. iCloud), using:

let store = CNContactStore()
let containerID = store.defaultContainerIdentifier()

...并且我可以(理论上)识别这样的本地容器-如果确实存在:

...and I can (theoretically) identify a local container like this — if one actually exists:

var allContainers = [CNContainer]()
    do {
        allContainers = try store.containersMatchingPredicate(nil)
        for container in allContainers {
            if container.type == CNContainerType.Local {
                print("Local container is: \(container.identifier)")
                break
            }
        }           
    } catch {
        print("Error fetching containers")
    }

但是不存在本地容器.关于如何将我的联系人存储在本地或在新的单独容器中的任何想法?

But no local container exists. Any ideas on how to store my contacts locally, or in a new separate container?

推荐答案

现在不推荐使用的AB API可以通过以下方式实现,但仍可以作为一种解决方法:

This was possible as follows with the now deprecated AB API, may still work as a workaround:

ABAddressBookRef addressBook = ABAddressBookCreateWithOptions( nil, nil );

ABRecordRef source = ABAddressBookGetSourceWithRecordID( addressBook, kABSourceTypeLocal );

ABRecordRef contact = ABPersonCreateInSource( source );

这篇关于如何强制将新的CNContact放入本地CNContainer?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-26 20:00