> ## Documentation Index
> Fetch the complete documentation index at: https://docs.median.co/llms.txt
> Use this file to discover all available pages before exploring further.

# Native datastore

> Store key-value data such as user preferences on the device or in the user's cloud account with the Native Datastore plugin and the JavaScript Bridge.

The Native Datastore plugin enables developers to persist key-value data such as user preferences and app settings directly on the user's device or in the cloud via the Median JavaScript Bridge.

This plugin supports both App Storage and Cloud Storage, giving you flexibility in how data is saved and accessed across sessions and devices.

> [!TIP]
>
> **Test  with our demo page**
>
> We host a ready-made demo page at
>
> that calls the  features through the Median JavaScript Bridge.
> Load it inside your Median app during development, either by setting it
> as your app's website URL in App Studio or by navigating to it from your
> own site, and use it to confirm  works on a real device before
> you add it to your own pages.
>
> The demo page only works inside a Median app with  enabled.
> Opened in a regular mobile or desktop browser, the native features
> won't respond. Remember to switch your app back to your own URL before
> release.

## Why use native datastore

* **User preference persistence**: Save settings like theme, language, and notification preferences that survive app restarts and WebView cache clears without relying on cookies.
* **Authentication state**: Store session tokens and login flags locally so users stay authenticated between app launches, with cloud storage restoring state after a reinstall.
* **Cross-device settings sync**: Use cloud storage to carry user preferences across devices without building a sync backend. When a user reinstalls or switches devices, their settings are restored automatically.
* **Offline-first apps**: Cache lightweight values like a user's account ID or last-known app state so the app can render meaningful content without a network connection.

## Storage backends

| Storage type | Android | iOS |
| - | - | - |
| App Storage | SharedPreferences | UserDefaults |
| Cloud Storage | SharedPreferences + Android Backup Service | Apple Keychain Services |

**App Storage** is **available offline** and persists on the device until the app is updated, reinstalled, or the device is upgraded. Ideal for session-specific or device-bound preferences.

**Cloud Storage** syncs data with the **user's cloud account**, making it accessible across multiple devices and app re-installations. Use Cloud Storage for settings that need to follow the user across devices.

## Storage limits

| Platform | App Storage | Cloud Storage |
| - | - | - |
| **Android** | No limit | Up to 5 MB |
| **iOS** | Up to 500 KB | Up to 16 MB |

Android cloud storage uses BackupManager, which schedules sync operations asynchronously. A precise sync time cannot be guaranteed as the system triggers a backup at some point after your app requests one. See [Google's BackupManager documentation](https://developer.android.com/reference/android/app/backup/BackupManager) for details.

## Implementation guide

### App storage

This option utilizes Android's SharedPreferences and iOS's UserDefaults to store the local app settings on the user's device. You will provide a key-value pair to save the app settings. You will then use the key to retrieve or delete the corresponding value.

**Note**: On Android the location of the App Storage (SharedPreferences) database file is `DATA/data/APP_PACKAGE_NAME/shared_prefs/user_preferences.xml`

#### Set command (app storage)

To save a value, use the following JavaScript Bridge command. To get status of this write operation, define a callback function in JavaScript and pass as `statuscallback` (Optional).

Median JavaScript Bridge

```javascript JavaScript theme={null}
median.storage.app.set({
  key: KEY,
  value: VALUE,
  statuscallback: statcb,
});

// Optional callback for status
function statcb(result) {
  if (result.status) {
    console.log(result.status);
  }
}
```

| Parameter | Type | Required | Description |
| - | - | - | - |
| `key` | String | Yes | The key to store the value under |
| `value` | String | Yes | The value to persist |
| `statuscallback` | Function | No | Receives a result object with a `status` property |

#### Get command (app storage)

To retrieve the saved settings define a callback function and call the JavaScript Bridge command below, providing the KEY of the saved setting and the callback that was defined. The status of the read operation is included as a parameter in the result variable as shown below.

Median JavaScript Bridge

```javascript JavaScript theme={null}
  // Callback
  median.storage.app.get({
    key: KEY,
    callback: cbRead
  });

  function cbRead(result) {
    console.log(result.data);    // the stored value
    console.log(result.status);  // operation status
  }

  // Promise method
  median.storage.app.get({ key: KEY })
    .then(function(result) {
      console.log(result.data);
      console.log(result.status);
  });
```

| Parameter | Type | Required | Description |
| - | - | - | - |
| `key` | String | Yes | The key to retrieve |
| `callback` | Function | Yes (callback style) | Receives `result.data` and `result.status` |

#### Delete command (app storage)

To delete a setting provide the KEY of the previously saved setting. The status of the delete operation can be accessed through the function passed as `statuscallback` (Optional).

Median JavaScript Bridge

```javascript JavaScript theme={null}
median.storage.app.delete({
  key: KEY,
  statuscallback: statcb,
});

// Optional callback for status
function statcb(result) {
  if (result.status) {
    console.log(result.status);
  }
}
```

#### Delete all (app storage)

Removes all stored key-value pairs from device-local storage.

Median JavaScript Bridge

```javascript JavaScript theme={null}
  median.storage.app.deleteAll({
    statuscallback: statcb  // optional
  });

  function statcb(result) {
    console.log(result.status);
  }
```

### Cloud storage

This option utilizes Android SharedPreferences combined with the native Android Backup Service and Apple Keychain Services to store the local app settings on a device. You will provide a key-value pair to save the app settings. You will use the key to retrieve or delete the settings.

**Note**: On Android, the location of the Cloud Storage (SharedPreferences) database file is `DATA/data/APP_PACKAGE_NAME/shared_prefs/user_preferences_backup.xml`

#### Set command (cloud storage)

To save a value, use the following JavaScript Bridge command. To get status of this write operation, define a callback function in JavaScript (Optional).

Median JavaScript Bridge

```javascript JavaScript theme={null}
median.storage.cloud.set({
  key: KEY,
  value: VALUE,
  statuscallback: statcb,
});

// Optional callback for status
function statcb(result) {
  if (result.status) {
    console.log(result.status);
  }
}
```

#### Get command (cloud storage)

To retrieve the saved settings define a callback function and call the JavaScript Bridge command below, providing the KEY of the saved setting and the callback that was defined. The status of the read operation is included as a parameter in the result variable as shown below.

Median JavaScript Bridge

```javascript JavaScript theme={null}
median.storage.cloud.get({
  key: KEY,
  callback: cbRead,
});

function cbRead(result) {
  if (result.data) {
    console.log(result.data);
  }
  if (result.status) {
    console.log(result.status);
  }
}

// Promise method
median.storage.cloud
  .get({
    key: KEY,
  })
  .then(function (result) {
    if (result.data) {
      console.log(result.data);
    }
    if (result.status) {
      console.log(result.status);
    }
  });
```

> [Learn more about using promises with the JavaScript Bridge](https://docs.median.co/docs/javascript-bridge.md#promises).

#### Delete command (cloud storage)

To delete a setting provide the KEY of the previously saved setting. The status of the delete operation can be accessed through the function passed as `statuscallback` (Optional).

Median JavaScript Bridge

```javascript JavaScript theme={null}
median.storage.cloud.delete({
  key: KEY,
  statuscallback: statcb,
});

// Optional callback for status
function statcb(result) {
  if (result.status) {
    console.log(result.status);
  }
}
```

#### Delete all (cloud storage)

Delete all saved settings from the device:

Median JavaScript Bridge

```javascript JavaScript theme={null}
median.storage.cloud.deleteAll({ statuscallback: statcb });

function statcb(result) {
  if (result.status) {
    console.log(result.status);
  }
}
```

### Callback status reference

All write and delete operations return status via `statuscallback`; `get` returns status via `callback`. Check `result.status` in your callback to confirm success or handle errors.

| `result.status` | Description |
| - | - |
| `success` | The operation completed successfully |
| `read-error` | The read operation failed |
| `write-error` | The write operation failed |
| `delete-error` | The delete operation failed |
| `preference-not-found` | The requested key does not exist in storage |

## Demo app

Try out the plugin on iOS and Android with our interactive demo apps.

## Frequently asked questions

> [accordion] Native Datastore functions are undefined or not working
>
> Ensure the JavaScript Bridge is enabled in App Studio and that your page has fully loaded before calling JavaScript Bridge functions. Verify that the plugin is enabled in your app configuration.

> [accordion] `get` returns preference-not-found even though a value was set
>
> Confirm that the `key` used in `get` exactly matches the `key` used in `set`. Keys are case-sensitive. Also verify that the value was successfully stored by checking the status returned to your `statuscallback`.

> [accordion] Cloud storage does not sync to another device
>
> On Android, BackupManager schedules sync operations asynchronously and the timing is system-determined and cannot be forced. On iOS, Keychain sync depends on the user's iCloud Keychain settings. Verify that iCloud Keychain is enabled on both devices.


## Related topics

- [Native datastore offline](https://docs.median.co/docs/native-datastore-offline.md)
- [Native functionality](https://docs.median.co/docs/native-functionality.md)
- [Changelog archive](https://docs.median.co/docs/changelog-archive.md)


This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.