turbo-vps-inappupdate
v0.1.1
Published
playstore app updates
Readme
turbo-vps-inappupdate
Google Play in-app update detector for React Native applications.
Installation
npm install turbo-vps-inappupdateUsage
import SpInAppUpdates, { IAUUpdateKind, IAUInstallStatus } from "turbo-vps-inappupdate";
import DeviceInfo from 'react-native-device-info'; // Required for getting app version
import { Platform } from 'react-native';
// Initialize the in-app updates instance
const inAppUpdates = new SpInAppUpdates(
false // isDebug - set to true for debugging
);
// Check for updates and start update process
inAppUpdates
.checkNeedsUpdate({ curVersion: DeviceInfo.getVersion() })
.then((result) => {
if (result.shouldUpdate) {
let updateOptions = {};
if (Platform.OS === "android") {
updateOptions = {
updateType: IAUUpdateKind.FLEXIBLE, // Options: IMMEDIATE or FLEXIBLE
};
}
inAppUpdates.startUpdate(updateOptions);
}
});
// Listen for update status changes
const updateListener = inAppUpdates.addStatusUpdateListener((downloadStatus) => {
if (downloadStatus.status === IAUInstallStatus.DOWNLOADED) {
// Install the downloaded update
inAppUpdates.installUpdate();
// Clean up listener after installation
inAppUpdates.removeStatusUpdateListener((finalStatus) => {});
}
});
// Cleanup function (use in useEffect cleanup or component unmount)
const cleanup = () => {
if (typeof unsubscribe === 'function') {
unsubscribe();
}
if (updateListener) {
inAppUpdates.removeStatusUpdateListener(updateListener);
}
};
// Don't forget to call cleanup when component unmounts
// cleanup();React Hook Example
import { useEffect } from 'react';
import SpInAppUpdates, { IAUUpdateKind, IAUInstallStatus } from "turbo-vps-inappupdate";
import DeviceInfo from 'react-native-device-info';
import { Platform } from 'react-native';
const useInAppUpdates = () => {
useEffect(() => {
const inAppUpdates = new SpInAppUpdates(false);
// Check for updates
inAppUpdates
.checkNeedsUpdate({ curVersion: DeviceInfo.getVersion() })
.then((result) => {
if (result.shouldUpdate) {
let updateOptions = {};
if (Platform.OS === "android") {
updateOptions = {
updateType: IAUUpdateKind.FLEXIBLE,
};
}
inAppUpdates.startUpdate(updateOptions);
}
});
// Listen for download completion
const updateListener = inAppUpdates.addStatusUpdateListener((downloadStatus) => {
if (downloadStatus.status === IAUInstallStatus.DOWNLOADED) {
inAppUpdates.installUpdate();
inAppUpdates.removeStatusUpdateListener((finalStatus) => {});
}
});
// Cleanup function
return () => {
if (updateListener) {
inAppUpdates.removeStatusUpdateListener(updateListener);
}
};
}, []);
};API Reference
Classes
- SpInAppUpdates: Main class for handling in-app updates
- Constructor:
new SpInAppUpdates(isDebug: boolean)
- Constructor:
Methods
checkNeedsUpdate(options): Check if app needs an update
options.curVersion: Current app version string- Returns: Promise with update availability info
startUpdate(updateOptions): Start the update process
updateOptions.updateType: Update type (IAUUpdateKind.FLEXIBLE or IAUUpdateKind.IMMEDIATE)
installUpdate(): Install a downloaded update
addStatusUpdateListener(callback): Listen for update status changes
- Returns: Listener reference for cleanup
removeStatusUpdateListener(listener): Remove status update listener
Enums
IAUUpdateKind: Update types
FLEXIBLE: User can continue using app while update downloadsIMMEDIATE: Update blocks app usage until complete
IAUInstallStatus: Installation status values
DOWNLOADED: Update has been downloaded and ready to install
Dependencies
This library requires react-native-device-info to get the current app version:
npm install react-native-device-infoPlatform Support
- ✅ Android (Google Play Store)
- ❌ iOS (Apple doesn't support in-app updates)
Contributing
See the contributing guide to learn how to contribute to the repository and the development workflow.
License
MIT
Made with create-react-native-library
