ICloud Support for Mac OS X Lion

In a documentless Mac OS X (10.7 Lion) application, I want to enable iCloud support so that I can exchange data between other instances of the same application on different Macs (and not on iOS devices). After a short look in the Apple documentation, I found that I should use the storage of key value values ​​in iCloud, since the document I want to download contains only an array of user objects (which have simple properties such as name (string), date (object dates), ...). This file is the only thing I want to upload to iCloud. In the application, I already implemented saving the file to disk using NSFileManager - (void)writeData:(NSData*)data toPath:(NSString *)path (or whatever it is, I forgot). It is loaded from the file using NSFileManager again (using - (NSData *)dataInFileAtPath:(NSString*)path or something else). The file is stored in a subdirectory in the application support directory. It is saved whenever a new element is added to the array or an element in the array is changed.

I was wondering if someone could provide a link to a tutorial or point me in the right direction, write this file to iCloud, and then upload it again to other instances of the same application? All the tutorials and documentation I found were for iOS only. Any help would be greatly appreciated!

Thank Advance

Ben

+4
source share
1 answer

Just use NSUbiquitousKeyValueStore , which is very similar to NSUserDefaults , except that it is recorded in iCloud. You should read the iCloud Storage section of the Mac App Programming Guide, but what you need to do can be as simple as including the rights on the Summary tab of your app target in Xcode, and then do the following:

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

Then, to get your data, just do

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

It should be noted here that to work with an array of user objects, these objects must implement the NSCoding protocol. This is just a matter of implementing two methods; here is a good tutorial .

PS You use the same APIs that you are developing for OS X or iOS, so there is no reason why you couldn’t just follow the iOS tutorial for storing iCloud.

+3
source

Source: https://habr.com/ru/post/1391261/