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;
    }

本文标签: 跳转到后端接口提示版本