简体   繁体   English

适用于Mac OS X Lion的iCloud支持

[英]iCloud Support in an application for Mac OS X Lion

Within a Mac OS X (10.7 Lion) Non-Document Based application, I want to include iCloud support so I can share data across other instances of the same application on other macs (not to iOS devices). 在Mac OS X(10.7 Lion) 非基于文档的应用程序中,我希望包含iCloud支持,以便我可以在其他mac(而不是iOS设备)上的同一应用程序的其他实例之间共享数据。 After surfing around the Apple documentation a bit, I've discovered I should use a key value list storage in iCloud, as the document I want to upload contains only an array of custom objects (that have simple properties such as a name (string), date (date object), ...). 稍微浏览Apple文档之后,我发现我应该在iCloud中使用键值列表存储,因为我要上传的文档只包含一个自定义对象数组(具有简单属性,如名称(字符串)) ,日期(日期对象),......)。 This file is the only thing I want to upload to iCloud. 这个文件是我唯一要上传到iCloud的文件。 Within the application, I have already implemented saving the file to the disk using NSFileManager 's - (void)writeData:(NSData*)data toPath:(NSString *)path (or whatever it was, I've forgotten). 在应用程序中,我已经实现了使用NSFileManager- (void)writeData:(NSData*)data toPath:(NSString *)path (或者其他任何东西,我已经忘记)将文件保存到磁盘。 It is loaded from the file using NSFileManager again (using - (NSData *)dataInFileAtPath:(NSString*)path , or whatever it was). 它再次使用NSFileManager从文件加载(使用- (NSData *)dataInFileAtPath:(NSString*)path ,或者它是什么)。 The file is stored in a subdirectory, in the user's Application Support directory. 该文件存储在用户的Application Support目录中的子目录中。 It is saved whenever a new item is added to the array, or an item in the array is modified. 只要将新项添加到数组中,或者修改了数组中的项,就会保存它。

I was wondering if anyone could provide a link to a tutorial, or point me in the right direction, to writing that file to iCloud, then downloading it again on other instances of the same application? 我想知道是否有人可以提供指向教程的链接,或指向正确的方向,将该文件写入iCloud,然后在同一应用程序的其他实例上再次下载? All the tutorials and documentation I have found have only been for iOS. 我发现的所有教程和文档都只适用于iOS。 Any help will be greatly appreciated! 任何帮助将不胜感激!

Thanks in Advance! 提前致谢!

Ben

Just use NSUbiquitousKeyValueStore , which is a lot like NSUserDefaults except that it's written to iCloud. 只需使用NSUbiquitousKeyValueStore ,它与NSUserDefaults非常相似,只不过它是写入iCloud的。 You should read the iCloud Storage section of the Mac App Programming guide, but what you need to do may be as simple as enabling entitlements in the Summary tab of your App Target in Xcode, and then doing something like: 您应该阅读Mac App Programming指南中的iCloud Storage部分,但您需要做的事情可能就像在Xcode中的App Target的Summary选项卡中启用权利一样简单,然后执行以下操作:

NSData *dataToStore = [NSKeyedArchiver dataWithRootObject:yourArray];
[[NSUbiquitousKeyValueStore defaultStore] setData:dataToStore forKey:@"yourKey"]; 

Then, to retrieve your data, just do 然后,要检索您的数据,就这样做

NSData *retrievedData = [[NSUbiquitousKeyValueStore defaultStore] dataForKey:@"yourKey"];
NSArray *retrievedArray = [NSKeyedUnarchiver unarchiveObjectWithData:retrievedData];

The one caveat here is that for this to work with an array of custom objects, these objects must all implement the NSCoding protocol . 这里要注意的是,为了使用一组自定义对象,这些对象必须都实现NSCoding协议 This is just a matter of implementing two methods; 这只是实现两种方法的问题; here's a good tutorial . 这是一个很好的教程

PS You use the same APIs whether you're developing for OS X or iOS, so there's no reason why you couldn't just follow an iOS tutorial for iCloud storage. PS无论您是针对OS X还是iOS进行开发,都使用相同的API,因此没有理由不按照iCloud存储的iOS教程进行操作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM