Created
November 25, 2021 18:40
-
-
Save sheerazam/faa227e1effb773f947bbfd73daa758d to your computer and use it in GitHub Desktop.
Implementing Force Update Feature using Firebase Remote Config in iOS
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// | |
// ForceUpdateChecker.swift | |
// OutApp | |
// | |
// Created by Sheeraz Ahmed Memon on 23/06/2018. | |
// Copyright © 2018 SamSoft. All rights reserved. | |
// | |
import Foundation | |
import Firebase | |
protocol OnUpdateNeededListener { | |
func onUpdateNeeded(updateUrl : String) | |
func onNoUpdateNeeded() | |
} | |
class ForceUpdateChecker { | |
static let TAG = "ForceUpdateChecker" | |
static let FORCE_UPDATE_STORE_URL = "force_update_store_url" | |
static let FORCE_UPDATE_CURRENT_VERSION = "force_update_current_version" | |
static let FORCE_UPDATE_REQUIRED = "force_update_required" | |
var listener : OnUpdateNeededListener | |
init(listener : OnUpdateNeededListener) { | |
self.listener = listener | |
} | |
func check(){ | |
let remoteConfig = RemoteConfig.remoteConfig() | |
let forceRequired = remoteConfig[ForceUpdateChecker.FORCE_UPDATE_REQUIRED].boolValue | |
print("\(ForceUpdateChecker.TAG) : forceRequired : \(forceRequired)") | |
if(forceRequired == true){ | |
let currentVersion = remoteConfig[ForceUpdateChecker.FORCE_UPDATE_CURRENT_VERSION].stringValue | |
print("\(ForceUpdateChecker.TAG) : currentVersion: \(currentVersion!)") | |
if(currentVersion != nil){ | |
let appVersion = getAppVersion() | |
if( currentVersion != appVersion){ | |
let url = remoteConfig[ForceUpdateChecker.FORCE_UPDATE_STORE_URL].stringValue | |
if(url != nil){ | |
listener.onUpdateNeeded(updateUrl: url! ) | |
} | |
} | |
else { | |
listener.onNoUpdateNeeded() | |
} | |
} | |
else { | |
listener.onNoUpdateNeeded() | |
} | |
} else { | |
listener.onNoUpdateNeeded() | |
} | |
} | |
func getAppVersion() -> String { | |
let versionNumber = Bundle.main.object(forInfoDictionaryKey: "CFBundleShortVersionString") as! String | |
let buildNumber = Bundle.main.object(forInfoDictionaryKey: "CFBundleVersion") as! String | |
let version = "\(versionNumber)(\(buildNumber))" | |
print("\(ForceUpdateChecker.TAG) : version: \(version)") | |
return version | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment