rn-app-upgrade

repository·master·Indexed 19 days ago

https://github.com/songxiaoliang/react-native-app-upgrade

A React Native library for managing application updates. It provides automated version detection, downloading, and installation for Android (compatible with Android 4.0+), and version checking with App Store redirection for iOS using the iTunes Lookup API.

Tokens
2K
Snippets
7
Records
9
Agent score
15%

What's inside rn-app-upgrade

  1. Handle iOS app updates

    master

    On iOS, use checkIOSUpdate to verify if a newer version of your app is available on the App Store. The function returns an object containing the update status code, a message, and the latest version string.

    import { checkIOSUpdate } from 'rn-app-upgrade';
    
    // appid: your App Store ID
    // currentVersion: your current app version
    const IOSUpdateInfo = await checkIOSUpdate(appid, currentVersion);
    
    /**
     * IOSUpdateInfo properties:
     * code: 
     *   -1: App not found or network error
     *    1: Latest version available
     *    0: No new version
     * msg: Status message
     * version: The latest version string
     */
    
    if (IOSUpdateInfo.code === 1) {
      // Logic to prompt user or redirect
    }
  2. Handle Android app updates

    master

    On Android, you can compare the current app version with a remote version (using versionCode from the library and RN.versionCode from React Native) to trigger a download.

    Use downloadApk to download and automatically install the new APK. You can monitor the download progress via a listener or the callback object.

    import { downloadApk, versionCode } from 'rn-app-upgrade';
    
    // Example: Triggering download if remote versionCode is higher
    if (res.versionCode > versionCode) {
      downloadApk({
        interval: 666, // Listen to upload progress event, emit every 666ms
        apkUrl: "https://xxxx.apk",
        downloadInstall: true,
        callback: {
          onProgress: (received, total, percent) => {
            // Handle progress
          },
          onFailure: (errorMessage, statusCode) => {
            // Handle error
          },
          onComplete: () => {
            // Handle completion
          },
        },
      });
    }
  3. Reference: rn-app-upgrade exported functions

    master

    The following functions are exported by the rn-app-upgrade module:

    {
      downloadApk: (options) => void, // Android: Downloads and installs APK
      versionName: string,            // Current version name
      versionCode: number,           // Current version code
      openAPPStore: () => void,      // iOS: Opens the App Store
      checkIOSUpdate: (appid, version) => Promise<IOSUpdateInfo>, // iOS: Checks for updates
      addDownLoadListener: () => void // Android: Adds a download listener
    }
  4. Download and install Android APKs with downloadApk()

    master

    On Android, use downloadApk() to download an APK file to the device and optionally trigger an immediate installation.

    Parameters (Options Object):

    • apkUrl (string): The direct URL to the APK file.
    • callback (object, optional): An object containing progress and status hooks:
      • onProgress(receivedSize, totalSize, percentage): Called during download. Sizes are formatted as strings (e.g., '1.2MB').
      • onFailure(errorMessage, statusCode): Called if the download fails.
      • onComplete(): Called when the download finishes.
    • interval (number, default: 250): The progress update interval in milliseconds.
    • downloadInstall (boolean, default: true): If true, the library will attempt to install the APK immediately after the download completes.

    Note: The APK is saved to a path provided by the native module RNUpgrade.downloadApkFilePath.

    import { downloadApk } from 'rn-app-upgrade';
    
    await downloadApk({
      apkUrl: 'https://example.com/app-latest.apk',
      downloadInstall: true,
      callback: {
        onProgress: (received, total, percent) => {
          console.log(`Downloaded: ${received} / ${total} (${percent}%)\n`);
        },
        onFailure: (err, status) => {
          console.error(`Download failed: ${err} (Status: ${status})`);
        },
        onComplete: () => {
          console.log('Download complete!');
        }
      }
    });
  5. Check for iOS App Store updates with checkUpdate()

    master

    On iOS, use checkUpdate(appId, version) to query the iTunes Lookup API for the latest version of your app. It compares the provided local version string against the version available in the App Store.

    Parameters:

    • appId (string): The unique App Store ID for your application.
    • version (string): The current local version of the app (e.g., '1.0.0').

    Returns an object with:

    • code (number):
      • 1: Update available.
      • 0: No update available.
      • -1: Error (e.g., app not found or network error).
    • msg (string): The release notes from the App Store if an update is available, or an error message.
    • version (string, optional): The new version string available on the App Store.
    import { checkUpdate } from 'rn-app-upgrade';
    
    const appId = 'YOUR_APP_ID';
    const currentVersion = '1.0.0';
    
    const result = await checkUpdate(appId, currentVersion);
    
    if (result.code === 1) {
      console.log('New version available:', result.version);
      console.log('Release notes:', result.msg);
    } else if (result.code === 0) {
      console.log('App is up to date.');
    } else {
      console.error('Update check failed:', result.msg);
    }