iOS Keychain Documentation - Version 1.3

Overview

Welcome to the iOS Keychain documentation page. iOS Keychain allows access to the iOS keychain on mobile devices inside of Unity. The Keychain allows users to store data in iCloud and the data will be synced between all of their devices if they have iCloud syncing enabled.

If a user uninstalls your application, the data stored in the Keychain is not removed. If they then re-install the app, their data will still be intact. If a user chooses to delete their iCloud data, all Keychain data is wiped.

Setting up the Plugin

UPGRADE GUIDE FROM VERSION 1.2.1 AND BELOW

1. Delete the old iOSKeychainPlugin folder in your project.

2. Prior to version 1.3, keychain data was stored using 2 static keys. That data can be retrieved using the following 2 keys.

// Recovers keychain data from version 1.2.1 and below			
string value1 = Keychain.GetValue("UserID");
string value2 = Keychain.GetValue("UserUUID");

The only required framework for iOS Keychain to work is Security.framework. Include this automatically in your builds if it isn't already by selecting the KeyChainPlugin.mm file in your project and in the inspector check the Security checkbox under Framework dependencies and hit Apply.

Using iOS Keychain

Storing a value using the plugin is very simple. Pass the Keychain class a string key and value and they are stored in the user's keychain. Items can then be retrieved using the storage key.

Plugin Methods

Keychain.SetValue(string key, string value) - Stores a string value in the Keychain.
Keychain.GetValue(string key) - Returns a string value from Keychain associated with that key.
Keychain.DeleteValue(string key) - Deletes the keychain value associated with that key.

Simple Example

First, store some user data in the keychain.

// implement the namespace at the top of your script
using FSG.iOSKeychain;			
			
string playerName = "Player1234";
int playerHealth = 123;
Keychain.SetValue("playerName", playerName);
Keychain.SetValue("playerHealth", playerHealth.ToString());

The users data can then be retrieved later as follows.

string playerName = Keychain.GetValue("playerName");
int playerHealth = int.Parse(Keychain.GetValue("playerHealth"));

JSON Example

A more efficient and easier method would be to use JSON and convert a player data class. Here's an short example below.

public class PlayerData
{
	public string playerName { get; set; }
	public int playerLevel { get; set; }
	public int[] completedLevels { get; set; }
	public string[] purchasedItems { get; set; }
	
	public PlayerData()
	{
		completedLevels = new int[0];
		purchasedItems = new string[0];
	}	
	public static void SaveData(PlayerData playerData)
	{
		string serializedData = JsonUtility.ToJson(playerData);
		Keychain.SetValue("playerData", serializedData);
	}
	public static PlayerData LoadSave()
	{
		string json = Keychain.GetValue("playerData");
		if (string.IsNullOrEmpty(json))
			return new PlayerData();
		return JsonUtility.FromJson(json);
	}
}

// Then you can simply call LoadSave() and SaveData() from anywhere in your application
PlayerData myPlayerData = PlayerData.LoadSave();
PlayerData.SaveData(myPlayerData);

If you have any questions or issues please email me at jschieck@gmail.com and thanks for your support!