flutter 开发中 ios端 app更新提示问题
问题描述:
flutter跨品台开发过程中,对于app的开发往往,都会有版本迭代,
而每次出了新版本的app之后,我们都希望及时的通知用户进行新版本的更新。
解决办法:
每次启动app之前,获取最新的版本号,在获取当前的版本号,进行对比,
如果获取的版本号大于当前的版本则,提示用户是否更新,若更新,则
跳转到苹果应用商店进行最新版的app下载安装。
这里有一个问题?就是最新版本从哪里获取呢?
办法一:
在数据中新建一张表用于存放,app的版本号,后端提供api 访问接口
方式二:(ios端)
我们可以通过,appstore(苹果应用商店)提供的接口获取到最新的版本号
https://itunes.apple.com/cn/lookup?id=你的appID 这个接口,
将返回一个json数据我们通过解这个json就可以拿到版本号
代码举例
```java
// csdn 不支持dart语言 只能用java代替了
// 版本接口
static const String Version = "https://itunes.apple.com/cn/lookup?id=你的appID 这个接口"
//版本更新方法
void isUpateVersion(){
Future future = new Future(()=>null);
future.then(
(_){
//获取当前版本
getCurVersion();
// 获取服务端版本
getServerVersion();
}
).then(
(_){
Future.delayed(
Duration(seconds:1),
(){
// 版本比较
if(compareVersion()){
_showUpadateInfo(
context:context,
title: "Update Version";
content: "1.修复已知问题\n2.持续优化";
);
}else{
// 进入app的逻辑
}
}
);
}
);
}
// 获取当前 版本
void getCurVersion() async{
PackageInfo packageInfo = await PackageInfo.fromPlatform();
String version = packageInfo.version;
setSate(
(){
// curVersio 实例属性
this.curVersion = Platform.isIOS ? 'ios_$version':'android_$version';
}
);
}
// 获取服务端版本
void getServerVersion() async{
await HttpRequest.getInstance().get(
"${Api.Version}",
successCallBack:(data){
this.serverVersion = json.decode(data)[0]["version"];
}
);
}
// 版本比较 是否更新
bool compareVersion(){
String serverVersion = this.serverVersion;
String curVersion = this.curVersion;
List serverVersionList - serverVersion.split(".");
List curVersionList = curVersion.split('_')[1].split('.');
bool flag = false;
if(serverVersionList.length == curVersionList.length){
for(var i = 0;i < curVersionList.length;i++){
if(int.parse(curVersionList[i]) < int.parse(serverVersionList[i])){
flag = true;
break;
}
}
}
return flag;
}
void _showUpdateInfo({context, String title, String content}){
showCupertinoDialog<int>(
context: context,
builder: (cxt){
return CupertinoAlertDialog(
title: Text(title),
content: Column(
children: <Widget>[
Container(
margin: EdgeInsets.only(
top: 5,
bottom: 15
),
child: Text(
content,
textAlign: TextAlign.left,
style: TextStyle(
fontFamily: "Times",
height: 1.2,
),
),
),
],
),
actions: <Widget>[
CupertinoDialogAction(
child: Text("Cancel"),
onPressed: () async {
showDialog();
},),
CupertinoDialogAction(
child:Text("Update"),
// 用户点击更新后 跳转的app 下载页面进行下载安装
onPressed: () async{
String new_version_url = 'https://apps.apple.com/cn/app/id你的appid';
if(await canLaunch(new_version_url)){
await launch(new_version_url);
}else{
}
showDialog();
},)
],);
});
}