How to force a new CNContact into a Local CNContainer?

六眼飞鱼酱① 提交于 2019-12-21 20:35:36

问题


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?

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?


回答1:


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 );



回答2:


The containersMatchingPredicate(nil) returns the default container only.

I have a similar problem. If icloud has been configured, it would only return the CNContainerTypeCardDAV type container else the local container.



来源:https://stackoverflow.com/questions/34587214/how-to-force-a-new-cncontact-into-a-local-cncontainer

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!