我正在编写一个应用程序,使用 CNContactStore 将通过 REST 和 JSON 获取的联系信息存储到一个容器中。我想将这些联系人与任何其他帐户分开,并且只存储在设备本地,但本地存储不存在,而且我找不到任何创建/激活联系人的方法?
我能够获取默认商店的 ID(如设备上配置的,例如 iCloud),使用:
let store = CNContactStore()
let containerID = store.defaultContainerIdentifier()
...我可以(理论上)识别这样的本地容器——如果确实存在的话:
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")
}
但是本地容器不存在。关于如何在本地或在新的单独容器中存储我的联系人有什么想法吗?
最佳答案
这在现在已弃用的 AB API 中是可能的,可能仍然可以作为一种解决方法:
ABAddressBookRef addressBook = ABAddressBookCreateWithOptions( nil, nil );
ABRecordRef source = ABAddressBookGetSourceWithRecordID( addressBook, kABSourceTypeLocal );
ABRecordRef contact = ABPersonCreateInSource( source );
关于xcode - 如何强制将新的 CNContact 放入本地 CNContainer?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34587214/