From 150ff0312a8204656ccb3333ed3398de7c976947 Mon Sep 17 00:00:00 2001 From: chanhihi Date: Thu, 29 Feb 2024 02:24:48 +0900 Subject: [PATCH 1/7] feat: versiong --- iBox/Resources/Version/VersionInfo.json | 5 +++++ iBox/Sources/Model/VersionInfo.swift | 12 ++++++++++++ 2 files changed, 17 insertions(+) create mode 100644 iBox/Resources/Version/VersionInfo.json create mode 100644 iBox/Sources/Model/VersionInfo.swift diff --git a/iBox/Resources/Version/VersionInfo.json b/iBox/Resources/Version/VersionInfo.json new file mode 100644 index 0000000..db2ba7c --- /dev/null +++ b/iBox/Resources/Version/VersionInfo.json @@ -0,0 +1,5 @@ +{ + "latestVersion": "1.2.0", + "minRequiredVersion": "1.1.0", + "updateUrl": "https://appstore.com/iBox" +} diff --git a/iBox/Sources/Model/VersionInfo.swift b/iBox/Sources/Model/VersionInfo.swift new file mode 100644 index 0000000..c6d11cc --- /dev/null +++ b/iBox/Sources/Model/VersionInfo.swift @@ -0,0 +1,12 @@ +// +// Versioning.swift +// iBox +// +// Created by Chan on 2/29/24. +// + +struct VersionInfo: Codable { + let latestVersion: String + let minRequiredVersion: String + let updateUrl: String +} From a6ed6907e98a255c335e5515de09536906c55f67 Mon Sep 17 00:00:00 2001 From: chanhihi Date: Sat, 2 Mar 2024 16:06:59 +0900 Subject: [PATCH 2/7] feat: add versioning handler --- .../Versioning/VersioningHandler.swift | 69 +++++++++++++++++++ 1 file changed, 69 insertions(+) create mode 100644 iBox/Sources/Versioning/VersioningHandler.swift diff --git a/iBox/Sources/Versioning/VersioningHandler.swift b/iBox/Sources/Versioning/VersioningHandler.swift new file mode 100644 index 0000000..b082c0f --- /dev/null +++ b/iBox/Sources/Versioning/VersioningHandler.swift @@ -0,0 +1,69 @@ +// +// VersioningHandler.swift +// iBox +// +// Created by Chan on 3/2/24. +// + +import UIKit + +class VersioningHandler { + + func checkAppVersion() { + let urlString = "https://my-json-server.typicode.com/42Box/versioning/db" + guard let url = URL(string: urlString) else { return } + + let task = URLSession.shared.dataTask(with: url) { [weak self] data, response, error in + guard let data = data, error == nil else { return } + + do { + let versionInfo = try JSONDecoder().decode(VersionInfo.self, from: data) + guard let latestVersion = versionInfo.version.first?.latestVersion, + let minRequiredVersion = versionInfo.version.first?.minRequiredVersion, + let updateUrl = versionInfo.url.updateURL else { return } + + DispatchQueue.main.async { + self?.compareVersion(latestVersion: latestVersion, minRequiredVersion: minRequiredVersion, updateUrl: updateUrl) + } + } catch { + print(error) + } + } + + task.resume() + } + + func compareVersion(latestVersion: String, minRequiredVersion: String, updateUrl: String) { + guard let appVersion = Bundle.main.infoDictionary?["CFBundleShortVersionString"] as? String else { return } + + if appVersion.compare(minRequiredVersion, options: .numeric) == .orderedAscending { + showAlertForUpdate(updateUrl: updateUrl, isMandatory: true) + } else if appVersion.compare(latestVersion, options: .numeric) == .orderedAscending { + showAlertForUpdate(updateUrl: updateUrl, isMandatory: false) + } + } + + + func showAlertForUpdate(updateUrl: String, isMandatory: Bool) { + guard let windowScene = UIApplication.shared.connectedScenes.first(where: { $0 is UIWindowScene }) as? UIWindowScene, + let rootViewController = windowScene.windows.first(where: { $0.isKeyWindow })?.rootViewController else { + return + } + + + let message = isMandatory ? "새로운 버전이 필요합니다. 업데이트 하시겠습니까?" : "새로운 버전이 있습니다. 업데이트 하시겠습니까?" + let alert = UIAlertController(title: "업데이트 알림", message: message, preferredStyle: .alert) + + alert.addAction(UIAlertAction(title: "업데이트", style: .default, handler: { _ in + if let url = URL(string: updateUrl), UIApplication.shared.canOpenURL(url) { + UIApplication.shared.open(url) + } + })) + + if !isMandatory { + alert.addAction(UIAlertAction(title: "나중에", style: .cancel)) + } + + rootViewController.present(alert, animated: true) + } +} From bbc9a631292815fd6631195da26fe3f80f0bc308 Mon Sep 17 00:00:00 2001 From: chanhihi Date: Sat, 2 Mar 2024 16:07:23 +0900 Subject: [PATCH 3/7] fix: version Info json --- iBox/Sources/Model/VersionInfo.swift | 21 ++++++++++++++++++--- 1 file changed, 18 insertions(+), 3 deletions(-) diff --git a/iBox/Sources/Model/VersionInfo.swift b/iBox/Sources/Model/VersionInfo.swift index c6d11cc..331a7cb 100644 --- a/iBox/Sources/Model/VersionInfo.swift +++ b/iBox/Sources/Model/VersionInfo.swift @@ -5,8 +5,23 @@ // Created by Chan on 2/29/24. // +// MARK: - VersionInfo struct VersionInfo: Codable { - let latestVersion: String - let minRequiredVersion: String - let updateUrl: String + let version: [Version] + let url: URLClass +} + +// MARK: - URLClass +struct URLClass: Codable { + let updateURL: String? + + enum CodingKeys: String, CodingKey { + case updateURL = "updateUrl" + } +} + +// MARK: - Version +struct Version: Codable { + let id: Int + let latestVersion, minRequiredVersion: String } From 1e336e64fea7d156f6fc5b8667ae7d689136d61b Mon Sep 17 00:00:00 2001 From: chanhihi Date: Sat, 2 Mar 2024 16:08:34 +0900 Subject: [PATCH 4/7] feat: add versioning handler in appdelegate --- iBox/Sources/AppDelegate.swift | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/iBox/Sources/AppDelegate.swift b/iBox/Sources/AppDelegate.swift index 1d9e30e..f21e00c 100644 --- a/iBox/Sources/AppDelegate.swift +++ b/iBox/Sources/AppDelegate.swift @@ -10,11 +10,11 @@ import CoreData @main class AppDelegate: UIResponder, UIApplicationDelegate { - - + let versioningHandler: VersioningHandler = VersioningHandler() func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool { - // Override point for customization after application launch. + + versioningHandler.checkAppVersion() return true } From da7a90678a92c458182a19d5c652e8344bd5dd38 Mon Sep 17 00:00:00 2001 From: chanhihi Date: Sat, 2 Mar 2024 16:08:51 +0900 Subject: [PATCH 5/7] test: mock versioninfo json --- iBox/Resources/Version/VersionInfo.json | 13 ++++++++++--- 1 file changed, 10 insertions(+), 3 deletions(-) diff --git a/iBox/Resources/Version/VersionInfo.json b/iBox/Resources/Version/VersionInfo.json index db2ba7c..5ee1ecb 100644 --- a/iBox/Resources/Version/VersionInfo.json +++ b/iBox/Resources/Version/VersionInfo.json @@ -1,5 +1,12 @@ { - "latestVersion": "1.2.0", - "minRequiredVersion": "1.1.0", - "updateUrl": "https://appstore.com/iBox" + "version": [ + { + "id": 1, + "latestVersion": "1.2.0", + "minRequiredVersion": "1.1.0" + } + ], + "url": { + "updateUrl": "https://appstore.com/iBox" + } } From 277ed9e8e82fd4ec7c848d0b362e1522d5061d2b Mon Sep 17 00:00:00 2001 From: chanhihi Date: Sat, 2 Mar 2024 16:21:20 +0900 Subject: [PATCH 6/7] chore: delete mock file --- iBox/Resources/Version/VersionInfo.json | 12 ------------ 1 file changed, 12 deletions(-) delete mode 100644 iBox/Resources/Version/VersionInfo.json diff --git a/iBox/Resources/Version/VersionInfo.json b/iBox/Resources/Version/VersionInfo.json deleted file mode 100644 index 5ee1ecb..0000000 --- a/iBox/Resources/Version/VersionInfo.json +++ /dev/null @@ -1,12 +0,0 @@ -{ - "version": [ - { - "id": 1, - "latestVersion": "1.2.0", - "minRequiredVersion": "1.1.0" - } - ], - "url": { - "updateUrl": "https://appstore.com/iBox" - } -} From 4c23452f7cbfbb1b347c5ba1bf022c969c792f69 Mon Sep 17 00:00:00 2001 From: chanhihi Date: Sat, 2 Mar 2024 16:22:20 +0900 Subject: [PATCH 7/7] feat: add versioning submodule --- .gitmodules | 3 +++ iBox/Resources/Version | 1 + 2 files changed, 4 insertions(+) create mode 100644 .gitmodules create mode 160000 iBox/Resources/Version diff --git a/.gitmodules b/.gitmodules new file mode 100644 index 0000000..46c791e --- /dev/null +++ b/.gitmodules @@ -0,0 +1,3 @@ +[submodule "iBox/Resources/Version"] + path = iBox/Resources/Version + url = https://github.com/42Box/versioning diff --git a/iBox/Resources/Version b/iBox/Resources/Version new file mode 160000 index 0000000..172ce29 --- /dev/null +++ b/iBox/Resources/Version @@ -0,0 +1 @@ +Subproject commit 172ce2939fccd76558f254adfb7b02535f97113c