앱을 업데이트하지 않더라도 상위 호환성을 갖춰 개발하는게 가장 이상적이지만 앱 출시 초반 단계에는 아무래도 오류나 기획, 사업적 차원에서 변동 사안이 많이 생겨 최대한 유저에게 업데이트를 권고하고 싶은 경우가 생기더라구요!
앱스토어와 현재 설치 버전을 비교하는 후 스토어 버전보다 낮을 경우 앱스토어 이동 Alert을 띄우는 코드입니다.
func latestVersion() -> String? {
let appleID = "1571087217" //**iTunes Connect > 나의 앱 > 확인하고 싶은 앱 클릭 > 앱정보 > 일반정보 > 번들ID**
guard let url = URL(string: "http://itunes.apple.com/lookup?id=\(appleID)"),
let data = try? Data(contentsOf: url),
let json = try? JSONSerialization.jsonObject(with: data, options: .allowFragments) as? [String: Any],
let results = json["results"] as? [[String: Any]],
let appStoreVersion = results[0]["version"] as? String else {
print("#########")
return nil
}
return appStoreVersion
}
let oldVersion = Bundle.main.infoDictionary?["CFBundleShortVersionString"] as? String,
guard let oldVersion = Bundle.main.infoDictionary?["CFBundleShortVersionString"] as? String,
let latestVersion = latestVersion() else { return }
let compareResult = oldVersion.compare(latestVersion, options: .numeric)
switch compareResult {
case .orderedAscending:
let appleID = "1571087217"
let appStoreOpenUrlString = "itms-apps://itunes.apple.com/app/apple-store/\(appleID)"
let alert = UIAlertController(title: "알림", message:
"업데이트 이후에 사용이 가능합니다.",
preferredStyle: UIAlertController.Style.alert)
alert.addAction(UIAlertAction(title: "이동", style: UIAlertAction.Style.default, handler: { (UIAlertAction) in
if let url = URL(string: appStoreOpenUrlString),
UIApplication.shared.canOpenURL(url) {
UIApplication.shared.open(url, options: [:], completionHandler: nil)
}
}))
self.present(alert, animated: true, completion: nil)
case .orderedDescending:
break
case .orderedSame:
break
}