iCloud需要xcode4.2 IOS5 sdk 请先做好准备工作:
1.需要传件一个新的app id,要是有了一个的话,保证着个app id 不是一个通配符的那种。
2.创建完成之后,你要做的是开启这项功能,就跟开发推送一样,然后在创建一个新的Provisional Profile
3.选择工程的summary,滚动到entitlement点击entitlements,xcode会自动的创建一个*.entitlements
4.点击创建的*.entitlements,分别把pist列表里的三个字段都添上内容,格式为 (Team_ID.com.yourcompany.icloudtest),不要把team_id 跟 app_id弄混了啊,team_id是你创建完Provisional的时候,在最前面显示的那10个字符。
5.然后就可以在delegate里面写下面的代码了
- - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
- {
- // Override point for customization after application launch.
- self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
- NSFileManager *fileManager = [NSFileManager defaultManager];
- // Team-ID + Bundle Identifier
- NSURL *iCloudURL = [fileManager URLForUbiquityContainerIdentifier:@"ZZZZ826ZZ2.com.yourcompany.icloudtest"];
- NSLog(@"%@", [iCloudURL absoluteString]);
- NSUbiquitousKeyValueStore *cloudStore = [NSUbiquitousKeyValueStore defaultStore];
- [cloudStore setString:[iCloudURL absoluteString] forKey:@"iCloudURL"];
- [cloudStore synchronize]; // Important as it stores the values you set before on iCloud
- UILabel *myLabel = [[UILabel alloc] initWithFrame:CGRectMake(0,round(self.window.bounds.size.height/4.0),self.window.bounds.size.width,round(self.window.bounds.size.height/8.0))];
- myLabel.font = [UIFont fontWithName:@"Marker Felt" size:round(self.window.bounds.size.width/20.0)];
- myLabel.numberOfLines = 4;
- myLabel.text =[ @"iCloudURL=" stringByAppendingFormat:@"%@", [cloudStore stringForKey:@"iCloudURL"]];
- myLabel.backgroundColor = [UIColor clearColor];
- myLabel.textColor = [UIColor whiteColor];
- myLabel.textAlignment = UITextAlignmentCenter;
- [self.window addSubview:myLabel];
- [self.window makeKeyAndVisible];
- return YES;
- }
DEMO下载: iCloudTest