강제 업데이트, 버전 노출 등에 쓰이는 OS별 최신 버전 확인 방법에 대해 소개한다.
pub.dev의 패키지를 활용하는 방법도 있지만, 많은 패키지 의존성을 좋아하지 않아서 http 통신으로 스토어에서 직접 버전을 확인하는 방법을 찾았다.
/// 앱 스토어 버전 확인
Future<Either<Failure, String?>> getAppStoreVersion(String bundleId) async {
const GetStoreVersionFailure failure = GetStoreVersionFailure('Error getting store version from App Store');
try {
Uri _uri = Uri.https(
'itunes.apple.com',
'/lookup',
{'bundleId': bundleId},
);
final http.Response _response = await http.get(_uri);
if (_response.statusCode == 200) {
final jsonObj = json.decode(_response.body);
String? _version = jsonObj['results'][0]['version'];
return Right(_version);
}
return const Left(failure);
} catch (e) {
return const Left(failure);
}
}
/// 플레이 스토어 버전 확인
Future<Either<Failure, String?>> getPlayStoreVersion(String packageName) async {
const GetStoreVersionFailure failure = GetStoreVersionFailure('Error getting store version from Google Play');
try {
final http.Response _response =
await http.get(Uri.parse('https://play.google.com/store/apps/details?id=$packageName'));
if (_response.statusCode == 200) {
RegExp regexp = RegExp(r'\[\[\["(\d+\.\d+(\.[a-z]+)?(\.([^"]|\\")*)?)"\]\]');
String? _version = regexp.firstMatch(_response.body)?.group(1);
return Right(_version);
}
return const Left(failure);
} catch (e) {
return const Left(failure);
}
}
스토어 별로 가끔 페이로드 값이 변경되는 것 같다. 값을 가져오는데 실패했을 때는 각자 원하는 방식으로 예외 처리하면 된다.