简单的iCloud键值存储Key-Value Data Storage
2015-12-09 00:00
639 查看
摘要: Key-Value Data Storage主要用于应用的小量非关键数据在多个设备间共享,支持的数据包括:NSString, NSDate, NSArray, NSData, Boolean, NSDictionary, NSNumber等对象
NSUbiquitousKeyValueStore类
NSUbiquitousKeyValueStore *keyStore =
[[NSUbiquitousKeyValueStore alloc] init];
[keyStore setString:@”Saved String” forKey:@"MyString"];
然后调用:
[k...
NSUbiquitousKeyValueStore类
NSUbiquitousKeyValueStore *keyStore =
[[NSUbiquitousKeyValueStore alloc] init];
[keyStore setString:@”Saved String” forKey:@"MyString"];
然后调用:
[k...
#import "iCloud01.h" @interface iCloud01 () @property (strong, nonatomic) IBOutlet UITextField *textfield;//上传到云端的数据 @property (strong, nonatomic) IBOutlet UILabel *displayValye;//下载同步到终端的数据 @property (nonatomic, strong) NSUbiquitousKeyValueStore * keyStore;//查询对象 @end @implementation iCloud01 - (void)viewDidLoad { [super viewDidLoad]; _keyStore = [[NSUbiquitousKeyValueStore alloc]init]; NSString * storedString = [self.keyStore stringForKey:@"paid"]; NSLog(@"Value is%@\n",storedString); if (storedString) { self.displayValye.text = storedString; } NSNotificationCenter * center = [NSNotificationCenter defaultCenter]; [center addObserver:self selector:@selector(ubiquitousKeyValueStoreDidChange) name:NSUbiquitousKeyValueStoreDidChangeExternallyNotification object:self.keyStore]; // Do any additional setup after loading the view. } - (IBAction)buttonPressed:(id)sender { //保存到云端 [_keyStore setString:self.textfield.text forKey:@"paid"]; [_keyStore synchronize];//进行同步 } - (void)ubiquitousKeyValueStoreDidChange { UIAlertController * controller = [UIAlertController alertControllerWithTitle:@"提示" message:@"iCloud关键字paid对应值有变化" preferredStyle:UIAlertControllerStyleAlert]; UIAlertAction * action = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleCancel handler:nil]; [controller addAction:action]; [self presentViewController:controller animated:YES completion:nil]; _displayValye.text = [_keyStore stringForKey:@"paid"]; } //- (void)addObserver:(NSObject *)observer forKeyPath:(NSString *)keyPath options:(NSKeyValueObservingOptions)options context:(void *)context - (void)didReceiveMemoryWarning { [super didReceiveMemoryWarning]; // Dispose of any resources that can be recreated. } /* NSUbiquitousKeyValueStore 它最多能存储1M的数据,最多可以同时存储1024个键(Key),单个值不能超过1M。 单个键的长度在UTF8编码时不能超过64个字节 当你注册NSUbiquitousKeyValueStoreDidChangeExternallyNotification监听时务必要指定默认的NSUbiquitousKeyValueStore对象(通过类方法defaultStore获取)为要监听的对象 同步方法synchronize并没有立刻将所有的设置都写入iCloud,而只是保存到磁盘。可能需要一段时间才能和iCloud同步。 */
相关文章推荐
- 解决Default storage engine (InnoDB) is not available导致mysql无法启动的修改办法
- 如何使用Visual Studio 2010在数据库中生成随机测试数据
- 对 jQuery 中 data 方法的误解分析
- javascript demo 基本技巧
- jquery load事件(callback/data)使用方法及注意事项
- jQuery中使用data()方法读取HTML5自定义属性data-*实例
- Javascript实现关联数据(Linked Data)查询及注意细节
- JQuery.Ajax()的data参数类型实例详解
- Select data from an Excel sheet in MSSQL
- js表数据排序 sort table data
- Mysql Data目录和 Binlog 目录 搬迁的方法
- mysql 卡死 大部分线程长时间处于sending data的状态
- ERROR 1406 : Data too long for column 解决办法
- mysql 的load data infile
- java复制文件和java移动文件的示例分享
- mysql Load Data InFile 的用法
- string中c_str(),data(),copy(p,n)函数的用法总结
- Oracle数据泵(Data Dump)使用过程当中经常会遇到一些奇奇怪怪的错误案例
- Oracle 11g 新特性 Flashback Data Archive 使用实例
- php实现SAE上使用storage上传与下载文件的方法