import Foundation
import UIKit
let appId = "0000" // 애플에 등록된 앱 ID
let title = "앱 업데이트" // 팝업 타이틀
let message = "새로운 버전이 출시되어 설치할 준비가 되었습니다." // 팝업 메세지
let okBtnTitle = "설치하기" // 설치 버튼
let cancelBtnTitle = "나중에" // 닫기 버튼
private var topViewController: UIViewController? {
guard var topViewController = UIApplication.shared.keyWindow?.rootViewController else { return nil }
while let presentedViewController = topViewController.presentedViewController {
topViewController = presentedViewController
}
return topViewController
}
enum UpdateType {
case normal
case force
}
class UpdateModule {
static func run(updateType: UpdateType) {
guard let url = URL(string: "https://itunes.apple.com/kr/lookup?id=\(appId)") else { return }
let request = URLRequest(url: url)
let session = URLSession(configuration: .default)
let task = session.dataTask(with: request, completionHandler: {
(data, _, _) in
guard let d = data else { return }
do {
guard let results = try JSONSerialization.jsonObject(with: d, options: .allowFragments) as? NSDictionary else { return }
guard let resultsArray = results.value(forKey: "results") as? NSArray else { return }
if resultsArray.count == 0 {
return
}
guard let storeVersion = (resultsArray[0] as? NSDictionary)?.value(forKey: "version") as? String else {
UserDefaults.standard.set(nil, forKey: "storeVersion")
UserDefaults.standard.synchronize()
return
}
UserDefaults.standard.set(storeVersion, forKey: "storeVersion")
UserDefaults.standard.synchronize()
guard let installVersion = Bundle.main.infoDictionary?["CFBundleShortVersionString"] as? String else { return }
guard installVersion.compare(storeVersion) == .orderedAscending else { return }
showAlert(updateType: updateType)
} catch {
print("Serialization error")
}
})
task.resume()
}
private static func showAlert(updateType: UpdateType) {
let alert = UIAlertController(title: title, message: message, preferredStyle: .alert)
let okAction = UIAlertAction(title: okBtnTitle, style: .default, handler: { Void in
guard let url = URL(string: "itms-apps://itunes.apple.com/app/id\(appId)") else { return }
if #available(iOS 10.0, *) {
UIApplication.shared.open(url, options: [:], completionHandler: nil)
} else {
UIApplication.shared.openURL(url)
// Fallback on earlier versions
}
})
alert.addAction(okAction)
if updateType == .normal {
let cancelAction = UIAlertAction(title: cancelBtnTitle, style: .cancel, handler: nil)
alert.addAction(cancelAction)
}
topViewController?.present(alert, animated: true, completion: nil)
}
}
UpdateModule.run(updateType: .normal)
해당방법으로 사용하면됨.
Swift-[Network 상태 확인] (0) | 2018.02.07 |
---|---|
Swift-[런처스크린 불러오기] (0) | 2018.01.16 |
IOS 앱스토어 등록전 준비 사항 (0) | 2017.08.02 |
IOS 런처 스크린 각 이미지 사이즈 정리 (0) | 2017.07.04 |
IOS 앱 버전 비교 (0) | 2017.05.16 |