react-native进行android系统app版本提示更新(无需后端接口)跳转到对应Google Play下载页
1.react native要想实现无需后端接口获取app版本信息要用到cheerio请点击 react native通过cheerio抓取google play商店里面app的版本号2.使用版本号进行对比提示更新import {Linking} from 'react-native';//设备信息import DeviceInfo from 'react-native-device-info';
·
1.react native要想实现无需后端接口获取app版本信息要用到cheerio
请点击 react native通过cheerio抓取google play商店里面app的版本号
2.使用版本号进行对比提示更新
import {Linking} from 'react-native';
//设备信息
import DeviceInfo from 'react-native-device-info';
import cheerio from 'cheerio';
componentDidMount() {
this._getVersion();
}
// 检查app更新
async _getVersion(){
let appid = "你app的applicationId";
const response = await fetch(`https://play.google.com/store/apps/details?id=${appid}&hl=en`,{
headers:{
"userAgent":"Mozilla/5.0 (Windows; U; WindowsNT 5.1; en-US; rv1.8.1.6) Gecko/20070725 Firefox/2.0.0.6"
},
referrer:"http://www.google.com"
})
const $ = cheerio.load(await response.text())
console.log("google play app版本号",$('div.hAyfc:nth-child(4) > span:nth-child(2) > div:nth-child(1) > span:nth-child(1)').text())
console.log("本机版本号", DeviceInfo.getVersion())
let version1 = DeviceInfo.getVersion();
let version2 = $('div.hAyfc:nth-child(4) > span:nth-child(2) > div:nth-child(1) > span:nth-child(1)').text();
let isUpdate = this.isUpdate(version1,version2);
console.log('版本对比',isUpdate)
if(isUpdate){
Linking.openURL(`https://play.google.com/store/apps/details?id=${appid}`).catch(err => console.error('An error occurred', err));
}
}
// 版本对比进行更新
isUpdate(version1,version2){
let arr1 = version1.split('.');
let arr2 = version2.split('.');
for(let i=0;i<arr1.length;){
if(arr1[i]==arr2[i]){
i++
}else{
if(arr1[i]<arr2[i]){
return true;
}else{
return false;
}
}
}
return false;
}
更多推荐



所有评论(0)