admin管理员组文章数量:1604673
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/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"
})
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/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;
}
版权声明:本文标题:react-native进行android系统app版本提示更新(无需后端接口)跳转到对应Google Play下载页 内容由热心网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:https://www.elefans.com/dianzi/1728464117a1159255.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论