Ios 尝试在iCloud中存储对象,但不要';我看不到任何行动

Ios 尝试在iCloud中存储对象,但不要';我看不到任何行动,ios,icloud,Ios,Icloud,我用于将对象存储到iCloud(键值存储)中 我的GUI只有一个触发按钮:pressButtonmethod 以下是相关代码: NSString *const keyStorageStr = @"keyForStore"; /* ... */ - (IBAction)pressButton:(UIButton *)sender { score++; [DemoiCloudViewController setScore: score]; scoreCountLab

我用于将对象存储到iCloud(键值存储)中

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

以下是相关代码:

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演示/

- (void)viewDidLoad
{
    [super viewDidLoad];

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

        isIcloudActive = true;

    } else {
        NSLog(@"No iCloud access");
    }
当我添加handler来监听所有内容时:

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统计信息:

请帮忙


谢谢,

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


原因:只有当更改来自iCloud(即,来自其他设备)时,才会出现您希望看到的通知。保存值时不会触发通知——为什么会触发?您已经知道已写入的值

是的,你说得对,我没想过要卸载。它按预期工作。非常感谢。