Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Notify users that there is an app update #542

Merged
merged 3 commits into from
Oct 23, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions HackIllinois.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -1301,7 +1301,7 @@
"$(inherited)",
"@executable_path/Frameworks",
);
MARKETING_VERSION = 2022.1.2;
MARKETING_VERSION = 2024.1.3;
ONLY_ACTIVE_ARCH = YES;
OTHER_SWIFT_FLAGS = "$(inherited) \"-D\" \"COCOAPODS\" -Xfrontend -warn-long-expression-type-checking=150";
PRODUCT_BUNDLE_IDENTIFIER = org.hackillinois.ios;
Expand Down Expand Up @@ -1332,7 +1332,7 @@
"$(inherited)",
"@executable_path/Frameworks",
);
MARKETING_VERSION = 2022.1.2;
MARKETING_VERSION = 2024.1.3;
ONLY_ACTIVE_ARCH = YES;
PRODUCT_BUNDLE_IDENTIFIER = org.hackillinois.ios;
PRODUCT_NAME = "$(TARGET_NAME)";
Expand Down
61 changes: 61 additions & 0 deletions HackIllinois/AppDelegate.swift
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,8 @@ class AppDelegate: UIResponder, UIApplicationDelegate {
_ = HILocalNotificationController.shared
GMSServices.provideAPIKey(String(bytes: obfuscatedApiKey.deobfuscated, encoding: .utf8)!)
HIApplicationStateController.shared.initalize()
// Check the app version and prompt the user to update if needed
checkAppVersion()
return true
}

Expand Down Expand Up @@ -125,3 +127,62 @@ extension AppDelegate {
tableViewAppearance.showsHorizontalScrollIndicator = false
}
}

// MARK: - App Update Check
extension AppDelegate {
// Check version of app against version of API
func checkAppVersion() {
let apiEndpointURL = URL(string: "https://adonix.hackillinois.org/version/ios")!

// Create an URLSession task to fetch the API version
let task = URLSession.shared.dataTask(with: apiEndpointURL) { data, response, error in
if let error = error {
NSLog("Error fetching API version: \(error)")
return
}
guard let data = data, let httpResponse = response as? HTTPURLResponse, httpResponse.statusCode == 200 else {
NSLog("Invalid response or status code from API endpoint")
return
}
do {
// Parse the JSON response to obtain the API version
if let json = try JSONSerialization.jsonObject(with: data, options: []) as? [String: Any],
let apiVersion = json["version"] as? String {
NSLog("API version is \(apiVersion)")
// Get the app's version from Info.plist
if let appVersion = Bundle.main.infoDictionary?["CFBundleShortVersionString"] as? String {
if appVersion.compare(apiVersion, options: .numeric) == .orderedAscending {
// App version is less than API version, prompt the user to update
s0phialiu marked this conversation as resolved.
Show resolved Hide resolved
self.showUpdateAlert()
}
}
}
} catch {
NSLog("Error parsing API response: \(error)")
}
}

task.resume()
}

func showUpdateAlert() {
let alert = UIAlertController(
title: "Update Required",
message: "A new version of the app is available. Please update to continue.",
preferredStyle: .alert
)
alert.addAction(UIAlertAction(title: "Update", style: .default, handler: { _ in
// Open the App Store page for app for the user to update
if let appURL = URL(string: "https://apps.apple.com/us/app/hackillinois/id1451755268") {
s0phialiu marked this conversation as resolved.
Show resolved Hide resolved
UIApplication.shared.open(appURL, options: [:], completionHandler: nil)
}
}))
DispatchQueue.main.async {
if let keyWindow = UIApplication.shared.windows.first(where: { $0.isKeyWindow }) {
if let rootViewController = keyWindow.rootViewController {
rootViewController.present(alert, animated: true, completion: nil)
}
}
}
}
}
10 changes: 0 additions & 10 deletions HackIllinois/FlowControllers/HIApplicationStateController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,6 @@ class HIApplicationStateController {
func initalize() {
window.makeKeyAndVisible()
resetPersistentDataIfNeeded()
reset2023IfNeeded()
recoverUserIfPossible()
recoverProfileIfPossible()
onboardingViewController.shouldDisplayAnimationOnNextAppearance = user == nil
Expand All @@ -51,15 +50,6 @@ class HIApplicationStateController {

// MARK: - API
extension HIApplicationStateController {
func reset2023IfNeeded() {
let didReset = UserDefaults.standard.object(forKey: "didReset2023") as? Bool ?? false
if !didReset {
_ = Keychain.default.purge()
HICoreDataController.shared.purge()
UserDefaults.standard.set(true, forKey: "didReset2023")
logoutUser()
}
}
func resetPersistentDataIfNeeded() {
guard !UserDefaults.standard.bool(forKey: HIConstants.APPLICATION_INSTALLED_KEY) else { return }
_ = Keychain.default.purge()
Expand Down
Loading