Coding Guidelines

The iCloud Fundamentals guide has a table that compares Document Storage to the Key-Value Store(Table 1-1). In contrary to Mobile Documents, the Key Value Store should be considered Always effectively available. Even if a user isn't connected to iCloud or in the offline mode, data will be uploaded later when it's possible.

Source

Write data:

iCloudManager.Instance.SetString ("TestStringKey", "Hello World");
iCloudManager.Instance.SetFloat ("TestFloatKey", 1.1f);

string msg = "hello world";
System.Text.UTF8Encoding  encoding = new System.Text.UTF8Encoding();
byte[] data = encoding.GetBytes(msg);
iCloudManager.Instance.SetData ("TestByteKey", data);

You can find all supported data types here.

Read Data

iCloudManager.OnCloudDataReceivedAction += OnCloudDataReceivedAction;
iCloudManager.Instance.RequestDataForKey ("TestFloatKey");

private void OnCloudDataReceivedAction (iCloudData data) {
    iCloudManager.OnCloudDataReceivedAction -= OnCloudDataReceivedAction;
	if(data.IsEmpty) {
		IOSNativePopUpManager.showMessage(data.Key, "data is empty");
	} else {
		IOSNativePopUpManager.showMessage(data.Key, data.StringValue);
	}
}	

New supported data types:

iCloudManager.Instance.SetInt("TestIntKey", 2);

List<object> myList = new List<object>();
myList.Add("test");
myList.Add("array");
iCloudManager.Instance.SetArray("TestArrayKey", myList);

Dictionary<object, object> myDict = new Dictionary<object, object>();
myDict.Add("first_key", "first_value");
myDict.Add("second_key", "second_value");
iCloudManager.Instance.SetDictionary("testDictKey", myDict);

iCloudManager.Instance.SetLong("TestLongKey", 34234234);

iCloudManager.Instance.SetUlong("TestUlongKey", 342323423);

Or more interesting example with object data type & anonymous function delegate

[Serializable]
public class MyClass
{
    public int level;
    public float timeElapsed;
    public string playerName;
}

MyClass myObject = new MyClass();
myObject.level = 1;
myObject.timeElapsed = 47.5f;
myObject.playerName = "Dr Charles Francis";

iCloudManager.Instance.SetObject(myObject);

iCloudManager.Instance.RequestDataForKey ("Test", (iCloudData data) => {
    if (data.IsEmpty) {
        Debug.Log(data.Key + " / " + "data is empty");
        return;
    } 

   MyClass myObject = data.GetObject<MyClass>();
   Debug.Log(myObject.level);
    
});

 

Handling Conflicts

Any device running your app and attached to a user’s iCloud account, can upload key-value changes to that account. To keep track of such changes, register for the HandleOnStoreDidChangeExternally notification during app launch. And use it to detect which keys changed externally and update the corresponding data in the user defaults database. Doing this, your user defaults database always contains the correct configuration values. The iCloud key-value store simply becomes a mechanism for ensuring that the user defaults database has the most recent changes. The following code snippet shows how to prepare your app to use the HandleOnStoreDidChangeExternally Action.

iCloudManager.OnStoreDidChangeExternally += HandleOnStoreDidChangeExternally;
void HandleOnStoreDidChangeExternally (System.Collections.Generic.List<iCloudData> changedData) {
	foreach(iCloudData data in changedData) {
		Debug.Log("Cloud data with key:  " + data.key + " was chnaged");
		//update value for key
	}
}

API References

iCloudManager

public class iCloudManager : ISN_Singleton<iCloudManager> {

	//Actions
	static event Action<ISN_Result> OnCloudInitAction;
	static event Action<iCloudData> OnCloudDataReceivedAction;
	static event Action<List<iCloudData>> OnStoreDidChangeExternally;

	//Public Methods
	void SetString(string key, string val);
	void SetFloat(string key, float val);
	void SetData(string key, byte[] val);
	void SetInt(string key, int val);
	void SetLong(string key, long val);
	void SetUlong(string key, ulong val);
	void SetObject(string key, object val);
	void SetArray(string key, List<object> val);
	void SetDictionary(string key, Dictionary<object, object> val)

	void RequestDataForKey(string key);
	void RequestDataForKey(string key, Action<iCloudData> callback)
}

iCloudData

public class iCloudData  {

	public string Key {get;}

	public string StringValue {get;}
	public float FloatValue {get;}
	public byte[] BytesValue {get;}
	public int IntValue {get;}
	public long LongValue {get;}
	public ulong UlongValue {get;}

	public List<object> ListValue {get;}
	public Dictionary<string, object> DictionaryValue {get;}

	public bool IsEmpty {get;}

	public T GetObject<T>()
}

 

The full implementation example can be found under the iCloudUseExample.cs script.