键值iCloud存储不起作用

时间:2013-04-26 11:02:13

标签: objective-c objective-c-blocks icloud

我正在使用KeyValue商店,但它无效。

首次启动时,它会检测是否在NSUbiquitousKeyValueStore中找到了密钥,否则它将创建一个新密钥并存储它。

NSUbiquitousKeyValueStore* keyStore = [NSUbiquitousKeyValueStore defaultStore];

NSDictionary* cloudDict = [keyStore dictionaryForKey: kCloudRootKey];

if (cloudDict != nil)
{
    NSDictionary* data1 = [keyStore dictionaryForKey: kCloudData1];
    NSDictionary* data2 = [keyStore dictionaryForKey: kCloudData2];
    NSDictionary* data3 = [keyStore dictionaryForKey: kCloudData3];
    NSLog(@"iCloud loaded");
}
else
{
    [self saveToCloud];
    NSLog(@"First time creating iCloud.");
}

- (void)saveToCloud
{
        NSUbiquitousKeyValueStore* keyStore = [NSUbiquitousKeyValueStore defaultStore];

        // data1,data2,data3 are all initialized
        NSMutableDictionary* cloudDict = [[NSMutableDictionary alloc] initWithCapacity: 3];
        [cloudDict setObject: data1 forKey: kCloudData1];
        [cloudDict setObject: data2 forKey: kCloudData2];
        [cloudDict setObject: data3 forKey: kCloudData3];

        [keyStore setDictionary: cloudDict forKey: kCloudRootKey];
        [keyStore synchronize];
        [cloudDict release];
}

每当用户按HOME按钮-applicationDidEnterBackground:(UIApplication*)application 然后我会再次致电-saveToCloud

我第一次在iPad上发布后,然后我切换到iPhone并首先启动它,我注意到它找不到cloudDict并且落入了其他语句中,这就是创建“第一次创建iCloud” 有谁知道我该怎么解决这个问题?

0 个答案:

没有答案