【发布时间】:2016-06-05 00:21:39
【问题描述】:
我想测试 iCloud 键值存储。以下是我采取的步骤:
1) 100美元左右购买苹果开发者账号,等待激活
2) 在开发者区域,我创建了一个 App ID、一个 iCloud 容器、一个配置文件(iOS 开发),并确保它知道我的个人设备。
3) 在 XCode 中创建了一个新的单视图 swift 应用
4) 在应用程序中添加以下代码:didFinishLaunchingWithOptions: 方法:
let keyStore = NSUbiquitousKeyValueStore.defaultStore()
#if (arch(i386) || arch(x86_64)) && os(iOS)
//let DEVICE_IS_SIMULATOR = true
keyStore.setString("testValue2", forKey: "testKey2")
let didSync = keyStore.synchronize()
print("synched: \(didSync)")
#else
//let DEVICE_IS_SIMULATOR = false
let didSync = keyStore.synchronize()
print("synched: \(didSync)")
if let theString = keyStore.stringForKey("testKey2") {
print("the string: \(theString)")
}
else {
print("did not find string with specified key")
}
#endif
5) 在5s模拟器上启动应用,确认keyStore.synchronize()返回true。
6) 等待 10 秒
7) 在我的 iPhone 6+ 上启动应用程序,确认 keyStore.synchronize() 返回 true。
8) 可悲的是,它打印出“没有找到具有指定键的字符串”
我做错了什么?
【问题讨论】:
-
删除对
synchronize的调用