Skip to content

Instantly share code, notes, and snippets.

@fahadjamal
Created February 23, 2017 06:18
Show Gist options
  • Save fahadjamal/c6ee03e790c1b3c22d4f04fc5a09ccb0 to your computer and use it in GitHub Desktop.
Save fahadjamal/c6ee03e790c1b3c22d4f04fc5a09ccb0 to your computer and use it in GitHub Desktop.
Notification about updating Enterprise App
// In App Delegate Add this
func applicationDidBecomeActive(_ application: UIApplication) {
// Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface.
let updateManager : UpdateManager = UpdateManager.sharedManager()
updateManager.pListUrl = String(format: "%@", GlobalConstants.AppDownloadPlistURL) // GlobalConstants.AppDownloadPlistURL
updateManager.versionUrl = String(format: "%@", GlobalConstants.AppVersionURL) //
updateManager.checkForUpdates()
}
{"CurrentVersion": "1.0.0}
struct GlobalConstants {
static let AppDownloadPlistURL = "itms-services://?action=download-manifest&url=https://gizbo.ae/buildingmaintenance/iosApp/manifest.plist"
static let AppVersionURL = "https://gizbo.ae/buildingmaintenance/iosApp/appVersion.json"
}
import UIKit
import Alamofire
class UpdateManager: NSObject {
var pListUrl : String!
var versionUrl : String!
var currentServerVersion : String!
static var shareManager: UpdateManager!
class func sharedManager() -> UpdateManager {
guard (shareManager != nil) else {
shareManager = UpdateManager()
return shareManager
}
return shareManager
}
func appVersion() -> String {
let infoDictionary : NSDictionary = Bundle.main.infoDictionary! as NSDictionary
let version :String = String(format: "%@", infoDictionary.value(forKey: "CFBundleShortVersionString") as! CVarArg)
let build : String = String(format: "%@", infoDictionary.value(forKey: "CFBundleVersion") as! CVarArg)
guard build.isEmpty else {
return String(format: "%@.%@", version, build) // "\(version).\(build)"
}
return String(format: "%@", version) //"\(version)"
}
func shouldAskForUpdate()->Bool {
let prefs : UserDefaults = UserDefaults.standard
guard (prefs.value(forKey: GlobalConstants.PreferenceAskUpdate) != nil) else {
return prefs.bool(forKey: GlobalConstants.PreferenceAskUpdate)
}
return true;
}
func disableAskUpdate() {
let prefs : UserDefaults = UserDefaults.standard
prefs.set(false, forKey: GlobalConstants.PreferenceAskUpdate)
prefs.synchronize()
}
func performUpdate() {
let prefs : UserDefaults = UserDefaults.standard
prefs.set(true, forKey: GlobalConstants.PreferenceAskUpdate)
let url : URL = URL(string: self.pListUrl)!
let thisApp : UIApplication = UIApplication.shared
thisApp.applicationIconBadgeNumber = 0
thisApp.openURL(url)
}
func splitVersionString(version : String)->NSMutableArray {
return NSMutableArray(array: version.components(separatedBy: "."))
}
func compareVersion(firstVersion : String, secondVersion : String) -> Int {
let fvArray : NSMutableArray = self.splitVersionString(version: firstVersion)
let svArray : NSMutableArray = self.splitVersionString(version: secondVersion)
while fvArray.count < svArray.count {
fvArray.add(NSNumber(value: 0))
}
while svArray.count < fvArray.count {
svArray.add(NSNumber(value: 0))
}
for i in 0..<fvArray.count {
let aNumber = fvArray.object(at: i)
let bNumber = svArray.object(at: i)
let a : Int = (aNumber as AnyObject).intValue
let b : Int = (bNumber as AnyObject).intValue
if a > b {
return 1
}
if (b > a) {
return -1;
}
}
return 0
}
func checkForUpdates() {
let urlString : String = String(format: "%@", versionUrl)
let serverBaseURL: URL = URL(string: urlString)!
let currentVersion : String = self.appVersion()
Alamofire.request(serverBaseURL, method: .post, parameters: nil, encoding: URLEncoding.default).responseJSON { (response:DataResponse<Any>) in
switch(response.result) {
case .success(_):
if response.result.value != nil {
let responseDictionary = response.result.value as! NSDictionary
print("responseDictionary is \(responseDictionary)")
let thisApp : UIApplication = UIApplication.shared
let serverVersion :String = String(format: "%@", responseDictionary.value(forKey: "CurrentVersion") as! CVarArg)
if self.compareVersion(firstVersion: serverVersion, secondVersion: currentVersion) <= 0 {
// make sure that we don't have a badge showing since there are no updates.
thisApp.applicationIconBadgeNumber = 0
self.currentServerVersion = currentVersion
print("The application is up to date.")
return
}
// we have determined that there is an update. We are going to ask the user if they would like to
// update immediately, but they may choose not to, so we will set a badge here to remind them later
// that there are pending updates.
thisApp.applicationIconBadgeNumber = 1
self.currentServerVersion = serverVersion
// if we have previously asked the user if they wanted to update and they refused, then we don't
// want to continue to bother them about it.
if !self.shouldAskForUpdate() {
print("There is a new version, but the user has opted to update manually later.")
return
}
let updateAlertController : UIAlertController = UIAlertController(title: "Update Available", message: "A new version is available. Update Now?", preferredStyle: .alert)
updateAlertController.addAction(UIAlertAction(title: "Ok", style: .default, handler: { (UIAlertAction) in
self.performUpdate()
}))
updateAlertController.addAction(UIAlertAction(title: "Cancel", style: .default, handler: {(UIAlertAction) in
self.disableAskUpdate()
}))
}
break
case .failure(_):
print(response.result.error!)
self.currentServerVersion = currentVersion
break
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment