Methods
Use these methods to manage updates outside React components -- in native module callbacks, background tasks, or any non-React context.
SwiftPatch Class
Quick Start
import { SwiftPatch } from '@swiftpatch/react-native';
const sp = new SwiftPatch({
deploymentKey: 'dk_your-key',
});
await sp.init();
const update = await sp.checkForUpdate();
if (update) {
await sp.downloadAndInstall(update);
sp.restart();
}
Constructor
new SwiftPatch(config?: SwiftPatchUserConfig)
Accepts the same options as withSwiftPatch. See Configuration.
Method Reference
| Method | Return Type | Description |
|---|---|---|
init() | Promise<void> | Initialize the native module. Call first. |
checkForUpdate() | Promise<ReleaseInfo | null> | Check for an available update |
download(release) | Promise<void> | Download a specific release |
install(release) | Promise<void> | Install a downloaded release |
downloadAndInstall(release) | Promise<void> | Download + install in one call |
restart() | void | Restart to apply the update |
rollback() | Promise<void> | Roll back to the previous bundle |
getCurrentBundle() | Promise<BundleInfo | null> | Get current bundle info |
clearPendingUpdate() | Promise<void> | Clear any pending update |
testConnection() | Promise<{connected, app?, organization?, reason?}> | Test server connectivity (no init() needed) |
Always call init() before any other method except testConnection().
Full Example
import { SwiftPatch } from '@swiftpatch/react-native';
const sp = new SwiftPatch({
deploymentKey: 'dk_your-key',
debug: true,
});
async function checkAndApplyUpdate() {
await sp.init();
const current = await sp.getCurrentBundle();
console.log('Current bundle:', current?.hash || 'original');
const update = await sp.checkForUpdate();
if (!update) {
console.log('No update available');
return;
}
console.log(`Update found: ${update.version} (${update.downloadSize} bytes)`);
if (update.isPatch) {
console.log('Differential patch available');
}
await sp.downloadAndInstall(update);
console.log('Update installed, restarting...');
sp.restart();
}
Standalone Functions
These are exported directly -- no SwiftPatch instance or provider needed.
restart()
Restart the app to apply a pending update.
import { restart } from '@swiftpatch/react-native';
restart();
getNativeConfig()
Read credentials from native config files (Info.plist / strings.xml). Useful for verifying your native setup.
import { getNativeConfig } from '@swiftpatch/react-native';
const config = await getNativeConfig();
console.log('Project ID:', config.projectId);
console.log('App Token:', config.appToken);
console.log('App ID:', config.appId);
console.log('Deployment Key:', config.deploymentKey);
console.log('Server URL:', config.serverUrl);
console.log('Public Key:', config.publicKey);
testConnection()
Test the connection to the SwiftPatch API without initializing the native module.
import { testConnection } from '@swiftpatch/react-native';
const result = await testConnection({
deploymentKey: 'dk_your-key',
serverUrl: 'https://api.swiftpatch.io/v1', // optional
});
if (result.connected) {
console.log('Connected to:', result.app?.name);
console.log('Organization:', result.organization?.name);
} else {
console.log('Connection failed:', result.reason);
}
getErrorDescription()
Get a readable description and troubleshooting steps for an error code.
import { getErrorDescription, SwiftPatchErrorCode } from '@swiftpatch/react-native';
const { description, troubleshooting } = getErrorDescription(
SwiftPatchErrorCode.NETWORK_ERROR
);
console.log(description);
// "Unable to connect to the update server."
console.log(troubleshooting);
// "Check your internet connection and verify the serverUrl in your config.
// If using a custom server, ensure it is reachable from the device."
Supported error codes: NETWORK_ERROR, DOWNLOAD_ERROR, VERIFICATION_ERROR, PATCH_ERROR, INSTALL_ERROR, CONFIG_ERROR, UNKNOWN_ERROR
Native Event Subscriptions
Four functions subscribe to events from the native layer (iOS/Android). These fire in real time, separate from the polled event queue (a queue where the SDK periodically checks for new events from native code).
import {
onDownloadProgress,
onRollback,
onVersionChanged,
onNativeEvent,
} from '@swiftpatch/react-native';
onDownloadProgress
Track download progress from the native downloader.
const unsubscribe = onDownloadProgress((progress) => {
console.log(`${progress.percentage}% — ${progress.downloadedBytes}/${progress.totalBytes}`);
});
// Clean up when done
unsubscribe();
onRollback
Listen for rollback events.
const unsubscribe = onRollback((reason) => {
console.log('Rollback occurred:', reason);
});
onVersionChanged
Listen for bundle version changes (after install + restart).
const unsubscribe = onVersionChanged((reason) => {
console.log('Bundle version changed:', reason);
});
onNativeEvent
Catch-all for every native event.
const unsubscribe = onNativeEvent((event) => {
console.log('Native event:', event);
});
Always call the returned unsubscribe function when you no longer need the subscription to avoid memory leaks.
Architecture Detection
Check whether SwiftPatch uses TurboModules (faster native module communication, replacing the old bridge) or the legacy bridge:
import { IS_TURBO_MODULE_ENABLED } from '@swiftpatch/react-native';
console.log('Using TurboModules:', IS_TURBO_MODULE_ENABLED);
// true = New Architecture (JSI — direct C++ to JavaScript communication, skipping the bridge)
// false = Legacy Bridge