我正在尝试将 iCloud 中的键/值对与我的应用一起使用。
我去了我的开发人员门户并检查了 iCloud 键/值对并选择了我的标识符之一。
我已经进入我的应用程序容量并添加了 iCloud 键/值存储,我可以看到它下面的标识,但它们是灰色的并且无法检查(为什么会这样?)。
在我的应用程序 info.plist 我没有做任何事情(我应该做吗?)。
在我的应用授权文件中,我添加了我在开发门户中检查的标识符。
这是我的应用程序中用于编码的内容。
我没有在 viewController.h 文件中添加任何内容
在我的 viewController.m 文件中添加
我找不到任何导入
@property (strong,nonatomic) IBOutlet UITextField *textField;
- (IBaction)saveKey:(id)sender;
我在我的故事板中创建了这些并将它们连接起来。
在 ViewDidLoad 中
_keyStore = [[NSUbiquitousKeyValueStore alloc] init];
NSString *storedString = [_keyStore stringForKey:@"MyString"];
if (storedString != nil){
_textField.text = storedString;
}
[[NsnotificationCenter defaultCenter] addobserver:self
selector: @selector(ubiquitousKeyValueStoreDidChange:)
name: NSUbiquitousKeyValueStoreDidChangeExternallyNotification
object:_keyStore];
然后分开
-(void) ubiquitousKeyValueStoreDidChange: (Nsnotification *)notification
{
UIAlertController *AlertView = [UIAlertController
alertControllerWithTitle:(@"Change detected")
message:@"iCloud key-value store change detected"
preferredStyle:UIAlertControllerStyleAlert];
UIAlertaction* okButton = [UIAlertaction
actionWithTitle:NSLocalizedString (@"OK",nil)
style:UIAlertactionStyleDefault
handler:^(UIAlertaction * action) {
[self dismissViewControllerAnimated:YES completion:NULL];
}];
[AlertView addaction:okButton];
[self presentViewController:AlertView animated:YES completion:nil];
_textField.text = [_keyStore stringForKey:@"MyString"];
dispatch_after(dispatch_time(DISPATCH_TIME_NOW,(int64_t)(3 * NSEC_PER_SEC)),dispatch_get_main_queue(),^{
[self dismissViewControllerAnimated:YES completion:NULL];
});
}
最后是我的按钮操作
- (IBaction)saveKey:(id)sender {
[_keyStore setString:_textField.text forKey:@"MyString"];
[_keyStore synchronize];
NSLog(@"YEP!!!");
}
我在 textField 中输入了一些内容并点击了 say 按钮,但没有任何反应。我关闭了应用程序,当我打开它时,textField 中没有任何内容。
然后我唯一能想到的是标识符是错误的,但如果我不能使用它,为什么我允许我在我的门户中选择它,但为什么它在我的应用程序中显示为灰色?
我非常感谢您的帮助。
谢谢