react-native-shared-container
v1.0.0
Published
Cross-app shared session storage for React Native (iOS App Group + Android signature-protected ContentProvider).
Maintainers
Readme
react-native-shared-container
shared session storage for React Native.
Share a small key/value store (auth tokens, user id, feature flags, …) between multiple apps published by the same team / signed with the same key, plus a per-app private store.
| Platform | shared scope backed by | local scope backed by |
| -------- | ------------------------ | ----------------------- |
| iOS | App Group container (NSUserDefaults suite) | NSUserDefaults.standard |
| Android | Exported, signature-protected ContentProvider | private SharedPreferences |
Only apps signed with the same key (Android) or sharing the same App Group (iOS) can
read each other's shared data, so the store is safe from third-party apps.
Installation
npm install react-native-shared-container
# or
yarn add react-native-shared-containeriOS
cd ios && pod installThen, for every app that should share data:
- Add the App Groups capability and enable the same group id in each app target
(e.g.
group.com.technoidentity.shared.container). - Either add that id as
SharedAppGroupIDin each app'sInfo.plist, or pass it at runtime viaconfigure({ appGroupId }).
Android
Autolinking registers the module and its ContentProvider. The provider authority and the
signature-level permission are scoped to each app's applicationId automatically — no manifest
edits are needed in the common case. All participating apps must be signed with the same key.
Usage
import {
configure,
getItem,
setItem,
removeItem,
appId,
type Scope,
} from 'react-native-shared-container';
// Call once at app start-up, before reading/writing the `shared` scope.
await configure({
// Android: the OTHER apps that share the session (your own id is added automatically).
peerAppIds: ['com.technoidentity.shared.container', 'com.technoidentity.shared.container.two'],
// iOS: the App Group container (optional if set via SharedAppGroupID in Info.plist).
appGroupId: 'group.com.technoidentity.shared.container',
});
// Cross-app, shared between all signed-together apps:
await setItem('shared', 'authToken', 'abc123');
const token = await getItem('shared', 'authToken'); // -> 'abc123' in every sibling app
// Per-app private:
await setItem('local', 'lastTab', 'home');
await removeItem('shared', 'authToken');
console.log('Running inside app:', appId);API
configure(config?): Promise<boolean>
| Field | Platform | Description |
| ------------ | -------- | ----------- |
| peerAppIds | Android | applicationIds of the sibling apps that share the session. Your own id is always included. |
| appGroupId | iOS | App Group container id for the shared scope. Falls back to SharedAppGroupID in Info.plist. |
getItem(scope, key): Promise<string | null>
setItem(scope, key, value): Promise<boolean>
removeItem(scope, key): Promise<boolean>
scope is 'local' (per-app private) or 'shared' (cross-app).
appId: string
The applicationId / bundle identifier of the running app.
How it works
- iOS — the
sharedscope opens anNSUserDefaultssuite for the configured App Group. Every app target that declares the same group reads and writes the same container. - Android — each app exposes an exported
ContentProvideratcontent://<applicationId>.sharedsession, locked behind asignature-level permission. On write, the value is fanned out to every configured peer's provider; on read, peers are queried in turn. Apps that aren't installed are skipped safely.
Example
A runnable two-app demo (App A / App B) lives in example/.
License
MIT
