componentDidMount(){
let that = this;
if(Platform.OS=="ios"){
this.getVersion();
}
}
getVersion() {
return fetch('https://itunes.apple.com/lookup?bundleId=您的bundleId')
.then((response) => response.json())
.then((responseJson) => {
console.log(responseJson.results[0].version,"苹果应用商店版本");
console.log(responseJson.results[0].trackViewUrl,"苹果应用商店的下载地址");
//用户安装app的版本和应用商店app的版本比较
if(parseFloat(this.state.nowVersion)<parseFloat(responseJson.results[0].version)){
console.log("可以更新");
this.setState({
storeUrl:responseJson.results[0].trackViewUrl,//应用商城下载的地址
updateVisible:true//提示用户更新的模态框
});
}else{
}
})
.catch((error) => {
console.error(error);
});
}
update(){
this.updateVisible(false);//关闭更新的模态框
//跳到应用商店下载页面
Linking.openURL(this.state.storeUrl).catch(err => console.error('An error occurred', err));
}
render() {
const { navigate } = this.props.navigation;
return (
{
Platform.OS=="ios"?
<Modal
animationType={"fade"}
transparent={true}
visible={this.state.updateVisible}
onShow={() => {}}
onRequestClose={() => {}} >
<View style={styles.modalStyle}>
<View style={styles.subView}>
<View style={styles.titleContain}>
<Image source={require('../../images/about/homeing_alert.png')} style={styles.titleImage}/>
<Text style={styles.titleText}>
版本更新!
</Text>
</View>
<Text style={styles.contentText}>
App Store有新版本,是否前往更新?
</Text>
<View style={styles.buttonView}>
<TouchableHighlight underlayColor='transparent'
style={styles.buttonStyle}
onPress={() => {
this.updateVisible(!this.state.updateVisible)
}}>
<Text style={styles.buttonText}>
取消
</Text>
</TouchableHighlight>
<TouchableHighlight underlayColor='transparent'
style={styles.buttonStyle}
onPress={this.update.bind(this)}>
<Text style={styles.buttonText}>
确定
</Text>
</TouchableHighlight>
</View>
</View>
</View>
</Modal>
:null
}
)
}