问题
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