iCloud的使用
2015-09-07 10:21
429 查看
iCloud的苹果推出的一套云服务系统,他的主要作用是为了能让苹果的各个类型的设备以及不同的app之间能实现数据共享。我最近做的一个项目就使用到了这个功能,本人从事蓝牙开发,现在公司的固件更新了,推出了新的app,但是,如果用户同时安装了新的app和之前的app就需要这两个app之间能共享数据。下面是我学习icloud的一些简单笔记:
1.要使用iCloud功能,必须在项目中打开iCloud,TARGETS---->Capabilities-->iCloud,如果你的程序需要真机测试和发布的,你还要在创建的证书中让它支持iCloud
2.下面是参考的简单代码:
1.要使用iCloud功能,必须在项目中打开iCloud,TARGETS---->Capabilities-->iCloud,如果你的程序需要真机测试和发布的,你还要在创建的证书中让它支持iCloud
2.下面是参考的简单代码:
#import "AppDelegate.h" @interface AppDelegate () @end @implementation AppDelegate /** * 文件的读写 * */ -(void)fileReadAndWrite{ NSFileManager *manager=[NSFileManager defaultManager]; NSURL *fileUrl=[manager URLForUbiquityContainerIdentifier:@"icloud"]; fileUrl=[fileUrl URLByAppendingPathComponent:@"Documents"]; //判断此路径是否存在 if ([manager fileExistsAtPath:[fileUrl path]]) { //存在 }else{ //不存在---->重新创建 [manager createDirectoryAtURL:fileUrl withIntermediateDirectories:YES attributes:[NSDictionary dictionaryWithObject:@"Vincent" forKey:@"name"] error:nil]; } fileUrl=[fileUrl URLByAppendingPathComponent:@"myInfo.txt"]; //写入数据 [@"好好学习,天天向上" writeToURL:fileUrl atomically:YES encoding:NSUTF8StringEncoding error:nil]; //读取数据 NSString *icloudStr=[[NSString alloc]initWithContentsOfURL:fileUrl encoding:NSUTF8StringEncoding error:nil]; NSLog(@"%@",icloudStr); } /** * key-Value的读写 * */ -(void)keyAndValue{ //先取得icloud的token NSUbiquitousKeyValueStore *myStore=[[NSUbiquitousKeyValueStore alloc]init]; //写入数据 [myStore setValue:@"Vincent" forKey:@"name"]; //在icloud的key-value操作数据之后必须在icloud同步 [myStore synchronize]; //读取数据 NSLog(@"%@",[myStore valueForKey:@"name"]); } - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions { // Override point for customization after application launch. //不管哪种读写方式,都需要先判断app是否支持iCloud服务 NSFileManager *manager=[NSFileManager defaultManager]; id token=[manager ubiquityIdentityToken]; if (token) { //支持 int i=arc4random()%2; switch (i) { case 0: //文件读写方式 [self fileReadAndWrite]; break; case 1: //key-Value方式 [self keyAndValue]; break; default: break; } }else{ //不支持 } return YES; }
相关文章推荐
- fatal error LINK1123:failure during conversion to COFF:file invalid or corrupt
- Hadoop Yarn 框架原理及运作机制
- Processing 练习(5)- Random bubbles fade out!!
- 关于JavaScript中function的两种创建方式的解析
- Meteor全栈开发平台 - 不仅仅是前端
- 微信app支付
- 进程通信-dbus(1)
- C3P0连接池配置信息记录备查
- dos下查看磁盘内存信息
- 常见的传输码型
- BUFFER CACHE之调整buffer cache的大小
- linux进程调度原理
- MySQL cmake安装
- IOS ARC项目使用非ARC文件
- 微信公众平台的后台开发流程(一)
- 高效学习Android动画
- hadoop 0.0.0.0/0.0.0.0:10020 connection refused
- 使用GDB调试core文件时的一些基本知识
- 斯坦福Developing iOS 8 Apps学习笔记(一)
- Android 使Volley完美支持自定义证书的Https