How to save game data to iCloud?

Hello, I'am an Unity game developer. Iam using custom Binary files to save game progress data (encoded strings, int, bool etc.). Now before I build my project/game to Xcode and upload it to the Appstore, I want the user to be able to save their progress to the iCloud so they can play on multiple devices and restore/override their game data if needed. However I can't find online a step for step guide on how to achieve this. The data that I would need to store for 1 user would probably not be more then 100 int/bool/float etc. I want to know what "Cloud system" I should use in my situation, and how to synchronize my binary files to iCloud. Help would very much be appreciated!

Accepted Reply

>I want the user....so they can play on multiple devices


If you want to access information from multiple devices with all the devices logged into the same Apple ID (i.e. what you wrote above) then you can use the iCloud key-value file.

https://developer.apple.com/library/archive/documentation/General/Conceptual/iCloudDesignGuide/Chapters/DesigningForKey-ValueDataIniCloud.html#//apple_ref/doc/uid/TP40012094-CH7-SW1


If you want users to be able to access the same data from multiple devices logged into different Apple ID's then you will need to use the public database of CloudKit.

https://developer.apple.com/documentation/cloudkit?language=objc

Replies

>I want the user....so they can play on multiple devices


If you want to access information from multiple devices with all the devices logged into the same Apple ID (i.e. what you wrote above) then you can use the iCloud key-value file.

https://developer.apple.com/library/archive/documentation/General/Conceptual/iCloudDesignGuide/Chapters/DesigningForKey-ValueDataIniCloud.html#//apple_ref/doc/uid/TP40012094-CH7-SW1


If you want users to be able to access the same data from multiple devices logged into different Apple ID's then you will need to use the public database of CloudKit.

https://developer.apple.com/documentation/cloudkit?language=objc

Do u know if there is a plugin or a tutorial step-by-step that shows u excatly how to implent, and do I need to learn Swift because I'am coding in C#.

I am sure there are tutorials. Search for NSUbiquitousKeyValueStore.


here is the Apple document:


https://developer.apple.com/library/archive/documentation/General/Conceptual/iCloudDesignGuide/Chapters/DesigningForKey-ValueDataIniCloud.html#//apple_ref/doc/uid/TP40012094-CH7-SW9


You don't need to learn Swift. It is unclear whether you will need to learn Objective C or whether your C# and the tutorial will work.

I think it is as simple as:

//to store a string
        NSUbiquitousKeyValueStore* store = [NSUbiquitousKeyValueStore defaultStore];
        [store setString:@"My string that I am storing" forKey:@"My String"];
        [store synchronize];

//to recover the string
    NSUbiquitousKeyValueStore* store = [NSUbiquitousKeyValueStore defaultStore];
    NSDictionary *iCloudDictionary=[store dictionaryRepresentation];
    NSString *recoveredString=[iCloudDictionary objectForKey:@"My String"];

Thank you for your reply! My last questions are: if I have a simple "public int number = 5;" in my C# script, where will that "number" be after building to Xcode, can I reference it by name and if so in what class do I have to implement ur code that u send me? And the last thing, how do I handle (or with what code) a situation where there are different versions stored on someones devices and iCloud and let the player chose between them.

I don't do C# but I guess that you need to create an NSNumber object:

[store setNumber:[NSNumber numberWithInt:number] forKey@"My Number"];

and recover it with

number=[[iCloudDictionary objectForKey:@"My Number"] intValue];

If you are writing a game in Unity, you will need to use a plugin that can communicate with CloudKit from C#. Two popular ones are the HovelHouse CloudKit plugin and the Prime31 one. I'm the author of the first. You can grab both on the asset store.

Hi did you manage to implement it? Could you please share the Unity script if so?

Hello, Has anyone implemented this ? Any tutorial or help will be much appreciated.