实现功能
- 自动检查本地APP版本是否最新
- 自动提醒用户升级
- 引导用户去 AppStore 或 Google Play 市场完成升级操作
- 当用户放弃本次升级后,在1天内不再提醒
实现逻辑
 async { var isUpdate = await UpdateModel.isUpdate(); if (isUpdate["isUpdate"] == 1) { buildUpdateShowDialog(context, isUpdate["appStoreUrl"]); } }
|
update_model.dart
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51
| class UpdateModel { static Future isUpdate() async { var timeEnd = DateTime.now().millisecondsSinceEpoch; var timeStart = UpdateApi.getUpgradeRemindLaterTime();
if (timeStart == 0 && timeEnd - timeStart >= getUpdateFrequency) {
Map appVersionData = await UpdateApi.getAppVersion(); var respAppVer = Map.from({ "isUpdate": 1, "appStoreUrl": "", }); var _serviceVersionCode; if (appVersionData != null) { if (Platform.isIOS) { _serviceVersionCode = appVersionData["versionCode"]["ios"]; respAppVer["appStoreUrl"] = SERVER_AppStore_URL; } else if (Platform.isAndroid) { _serviceVersionCode = appVersionData["versionCode"]["android"]; respAppVer["appStoreUrl"] = SERVER_GooglePlay_URL; } } respAppVer["isUpdate"] = await _checkVersionCode(_serviceVersionCode); return respAppVer; } }
static _checkVersionCode(String appSerCode) { String _appLocalCodeStr = UpdateApi.getAppLocalCode(); final _appSerCode = Version.parse(appSerCode); final _appLocalCode = Version.parse(_appLocalCodeStr); if (_appSerCode > _appLocalCode) { return 1; } else { return 0; } } }
|
源代码
Github