📜 ⬆️ ⬇️

ICloud integration using the example of Cut the Rope

Hi habra-resident. Today, I would like to tell you about my experience of integrating iCloud into the Cut the Rope game.

This post was inspired by the feedback from one user who came to the email address of technical support:
"I didn’t need any help, I wanted to ask you something ... How did you
run iCloud Cut the Rope even if it is not a universal application? I
alternate with iPhone and iPad.
Unfortunately, no other developer does this.
applications, so I wonder ... you are a magician you or others are not
willing to do anything? Thank you for your
beautiful game and support it for long. Hello.

I do not know how extraordinary the fact that the iPad and iPhone versions of the application use one game progress, but today I will tell how to achieve this. Especially since it is very simple. I will also talk about some of the problems and their solutions that we encountered during integration.

What is iCloud?
iCloud is a free cloud service, for data storage, with support for push-technologies. Created by Apple. In iOS, it appeared, starting with the fifth version. And in Mac OS starting from version 10.7 (Lion).
')
Design issues
We wanted the use of iCloud for the user to be as transparent as possible. Therefore, by default, we disabled this feature and made a special item in the menu to enable it:

This is also done so that each family member can independently go through all the boxes on his iOS device using the same Apple ID.

Since the data update occurs asynchronously, we decided to add the following notification about the presence of saving in iCloud:

This notification allows you to easily and easily use the progress that already exists in iCloud. It is shown only once, after the first synchronization. Moreover, synchronization occurs regardless of the settings in the menu. Customization only affects whether we apply the resulting progress to the local one.

It was decided to make progress on the levels only from the best results from each device. Thus, you can go through one half of the box on one device and the other on the second and get full progress on the box on both devices through several synchronizations.

The game Cut the Rope has the ability to reset the game progress. And we thought about whether to reset the progress saved in iCloud with it. We decided that it was not worth it. We thought that the latest and best result for each level will always be stored in iCloud so that the user can restore it at any time, regardless of any external factors. In case the player wants to reset progress and go through the whole game from scratch, he can turn off iCloud synchronization.

Integration
There are two ways to integrate iCloud into an iOS application:
iCloud document storage (saving files in iCloud user account) and
iCloud key-value data storage ( key-value data storage ).

I will talk about the second option, because in our game, game progress is saved in this way - level-result pair.
  1. First of all, you need to enable iCloud support in the application on the developer.apple.com website and rewrite the corresponding profiles.
  2. Include entitlements in the project. In xcode version 4.2, this is done as follows:
    In the Project navigator, double click on the project, select the desired target and on the Summary tab in the Entitlements section, turn on the checkbox opposite the “Enable Entitlements” line.
  3. In the iCloud Key-Value Store field, specify the container identifier used in the application. This identifier may be the same for different applications. This is how the possibility of using one progress for two different applications is realized. As a result, for our applications the identifier will look like “com.zeptolab.ctr”, for full versions (iPhone and iPad). And "com.zeptolab.ctrlite", for free versions. In our case, we had to split the progress files, because in the free versions there is a different number and order of levels.
    Here are the correct settings in our project:

  4. In the application code, add listening for data change messages, for example:
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(notification:) name:NSUbiquitousKeyValueStoreDidChangeExternallyNotification object:[NSUbiquitousKeyValueStore defaultStore]];
    And do not forget to unsubscribe from these messages when they are no longer needed:
    [[NSNotificationCenter defaultCenter] removeObserver:self name:NSUbiquitousKeyValueStoreDidChangeExternallyNotification
    object:[NSUbiquitousKeyValueStore defaultStore]];
  5. Accordingly, in the method
    -(void)notification:(NSNotification *)notification
    handle the situation of obtaining new data.
  6. Working with NSUbiquitousKeyValueStore using a dictionary as an example. Access to the dictionary is as follows:
    NSUbiquitousKeyValueStore* store = [NSUbiquitousKeyValueStore defaultStore];
    NSDictionary* storedDict = [store dictionaryForKey:GAME_PROGRESS];
    And so it can be synchronized:
    NSUbiquitousKeyValueStore* store = [NSUbiquitousKeyValueStore defaultStore];
    [store setDictionary:dict forKey:GAME_PROGRESS];
    [store synchronize];

Note
The NSUbiquitousKeyValueStore interface is very similar to NSUserDefaults. But the second can not be completely replaced by the first. The application should always be locally saved current game progress.

Old firmware support :
Since iCloud appeared only starting with iOS 5, for compatibility with earlier versions this functionality will have to be disabled.
This is done as follows:

Thanks for attention. I hope someone finds this article interesting.

useful links
iCloud Programming Guide
NSUbiquitousKeyValueStore Class Reference

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


All Articles