gpt4 book ai didi

ios - 尝试在 iCloud 中存储对象但看不到任何操作 iOS

转载 作者:行者123 更新时间:2023-11-29 03:07:27 25 4
gpt4 key购买 nike

我使用 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

请帮忙,

谢谢

最佳答案

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

原因:您期望看到的通知仅在更改来自 iCloud(即源自其他设备)时才会发生。当您保存值时,通知不会被触发——为什么会这样?您已经知道已写入的值!

关于ios - 尝试在 iCloud 中存储对象但看不到任何操作 iOS,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22609506/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com