尝试在iCloud中存储Object但是没有看到iOS的任何动作

时间:2014-03-24 12:33:45

标签: ios icloud

我使用this tutorials将对象存储到iCloud(键值存储)中。

我的GUI只有一个触发按钮:pressButton方法

以下是相关代码:

NSString *const keyStorageStr = @"keyForStore";

/* ... */

- (IBAction)pressButton:(UIButton *)sender {

    score++;

    [DemoiCloudViewController setScore: score];

    scoreCountLabel.text = [NSString stringWithFormat:@"%d", score];

    // store to preferences
    NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
    [defaults setInteger:score forKey:keyStorageStr];

    // store to iCloud 
    [self setInteger:score forKey:keyStorageStr];
}

- (void)setInteger:(int)value forKey:(NSString*)key {
    [self setObject:[NSNumber numberWithInt:value] forKey:key];
}

- (void)setObject:(id)value forKey:(NSString *)key {

    NSUbiquitousKeyValueStore *store = [NSUbiquitousKeyValueStore defaultStore];
    if (store) {
        [store setObject:value forKey:key];
        //[store synchronize];
    }    
}

这是我的经纪人:

NSUbiquitousKeyValueStore* store = [NSUbiquitousKeyValueStore defaultStore];
    if (store) {

        [[NSNotificationCenter defaultCenter] addObserver:self
                                                 selector:@selector(storeChanged:)
                                                     name:NSUbiquitousKeyValueStoreDidChangeExternallyNotification
                                                   object:store];

        [store synchronize];
    }

- (void)storeChanged:(NSNotification*)notification {/* .. never called .. */}

我在这里获得了适当的价值:file://localhost/private/var/mobile/Library/Mobile%20Documents/76Z3CR95HF~com~wifi~icloud~WiFi-iCloud-Demo/

- (void)viewDidLoad
{
    [super viewDidLoad];

    ubiq = [[NSFileManager defaultManager]
                   URLForUbiquityContainerIdentifier:nil];
    if (ubiq) {
        NSLog(@"iCloud access at %@", ubiq);

        isIcloudActive = true;

    } else {
        NSLog(@"No iCloud access");
    }

当我添加处理程序来监听所有内容时:

NSNotificationCenter *notifyCenter = [NSNotificationCenter defaultCenter];
    [notifyCenter addObserverForName:nil
                              object:nil
                               queue:nil
                          usingBlock:^(NSNotification* notification){
                              // Explore notification
                              NSLog(@"Notification found with:"
                                    "\r\n     name:     %@"
                                    "\r\n     object:   %@"
                                    "\r\n     userInfo: %@",
                                    [notification name],
                                    [notification object],
                                    [notification userInfo]);
                          }];

我明白了:

name:     _UIApplicationSystemGestureStateChangedNotification

     object:   (null)

     userInfo: (null)

来自xCode iCloud统计数据:

enter image description here

请帮忙,

谢谢,

1 个答案:

答案 0 :(得分:1)

如果删除应用并重新运行,会发生什么?你能从iCloud中检索价值吗?除非你没有提到其他一些配置问题,否则我猜你可以。

原因:您希望看到的通知仅在更改来自iCloud时发生(即,来自其他设备)。保存值时,将不会触发通知 - 为什么会这样?你已经知道已写的价值了!