preface

In general, React-Native projects are integrated into Ios or Android apps and we do not have the right to perform App updates in RN projects. Sometimes rn projects rely on the latest version of the App for some of their functionality, and users need to be prompted for a version update and directed to the App store. The Android app store is not unified and cannot guide users correctly. However, on Ios, users can be quickly directed to the AppStore to download the latest version of the App. The following method takes JINGdong App as an example:

Determine whether jingdong App needs version upgrade

1) Get the version of the current reference

function getClientVersion() {
    return new Promise((resolve) = > {
        JDNativeSystem.getClientVersion().then((res) = > {
            resolve(res || ' ')
        }).catch((err) = > {
            resolve(' ')
            console.log('JDNativeSystem.getClientVersion9 err! ', err)
        })
    
    })
}
Copy the code

2) Determine whether the current version needs to be upgraded

const miniVersion = '9.3.2'
// Compare version numbers
function compareVersion(compareVersion, currentVersion) {
	var v1 = version2Num(compareVersion)
	var v2 = version2Num(currentVersion)
	if (v1 > v2) {
		return -1
	} else if (v1 == v2) {
		return 0
	} else {
		return 1
	}

	function version2Num(version) {
		// Convert the version number to a number
		var c = version.toString().split('. ')
		var num_place = [""."0"."00"."000"."0000"]
		var r = num_place.reverse()
		for (var i = 0; i < c.length; i++) {
			var len = c[i].length
			c[i] = r[len] + c[i]
		}
		var res = c.join(' ')
		return res
	}
}
// Whether an upgrade is required
function isNeedUpgrade() {
	return new Promise((resolve) = > {
		getClientVersion().then((version) = > {
			if (version) {
				const result = compareVersion(miniVersion, version)
				if (result >= 0) { // No upgrade required
					resolve(false)
				}
			}
			resolve(true) // Upgrade required})})}Copy the code

If you need to upgrade the App version, the dialog box prompts you to download the latest App version

Obtain the App ID of JINGdong App in AppStore

1) Enter the Apple App Store www.apple.com/app-store/ 2) search for JINGdong to obtain the app list data 3) Click jingdong to jump to the jingdong details page, follow the link of jingdong details page apps.apple.com/us/app/%E4%… , you can obtain the JINGdong application ID — 414245413

Obtain the download address of JINGdong App in Apple Store

// Obtain the download address of JINGdong app in Apple Store
function getJDAPPStoreInfo() {
	const JDAppStoreId = 414245413
	fetch(`https://itunes.apple.com/CN/lookup?id=${JDAppStoreId}`)
		.then((response) = > response.json())
		.then((res) = > {
			if (res && res.results && res.results.length) {
				this.JDAppVersion = res.results[0].version // Latest version of JD App
				this.JDAppStoreUrl = res.results[0].trackViewUrl // The download address of jingdong App in Apple App Store
			}
		}).catch((err) = > {

		console.log('getJDAPPStoreInfo err! ', err)
	})

}
Copy the code

Jump to jingdong App download page in App store

import { Linking } from 'react-native'
Linking.openURL(this.JDAppStoreUrl).catch((err) = > {
    console.log(err, 'jump app store err! ')})Copy the code