Skip to content

Instantly share code, notes, and snippets.

@kreeger
Last active August 29, 2015 14:23
Show Gist options
  • Save kreeger/92d640e7737af4c9af3e to your computer and use it in GitHub Desktop.
Save kreeger/92d640e7737af4c9af3e to your computer and use it in GitHub Desktop.
Checks for a new version from a server and prompts the user to install it. Great for enterprise builds.
//
// EAVersionChecker.swift
// Copyright (c) 2015 Oven Bits. All rights reserved.
//
import Foundation
class EAVersionChecker {
var versionManifestURL: String = ""
var currentLocalVersion: String { return "\(localSVS).\(localBuild)" }
private var infoDictionary: [NSObject:AnyObject]! { return NSBundle.mainBundle().infoDictionary! }
private var localSVS: String { return infoDictionary["CFBundleShortVersionString"] as! String }
private var localBuild: String { return infoDictionary["CFBundleVersion"] as! String }
private var session = NSURLSession(configuration: NSURLSessionConfiguration.defaultSessionConfiguration())
static let sharedChecker: EAVersionChecker = EAVersionChecker()
func checkForUpdates(completion: (NSURL?, NSError?) -> Void) {
if let url = NSURL(string: versionManifestURL) {
let task = session.dataTaskWithURL(url) { data, resp, error in
if let error = error {
completion(nil, error)
return
}
self.handleServerResponse(data, completion: completion)
}
task.resume()
return
}
completion(nil, nil)
}
private func handleServerResponse(data: NSData, completion: (NSURL?, NSError?) -> Void) {
if let parsed = NSJSONSerialization.JSONObjectWithData(data, options: .AllowFragments, error: nil) as? [NSObject:AnyObject] {
let json = JSON(parsed)
let version = json["latestVersion"].string
if !self.versionStringIsGreaterThanInternalVersion(version) {
DDLogInfo("Version retrieved from server (\(version)) is older, or current.")
completion(nil, nil)
return
}
if let updateURLString = json["updateUrl"].string, updateURL = NSURL(string: updateURLString) {
DDLogWarn("Version retrieved from server (\(version)) is newer than \(self.currentLocalVersion). Requesting update.")
completion(updateURL, nil)
return
}
} else {
let jsonString = NSString(data: data, encoding: NSUTF8StringEncoding)
DDLogError("Encountered error parson JSON: \(jsonString)")
}
}
private func versionStringIsGreaterThanInternalVersion(versionString: String?) -> Bool {
if let result = versionString?.compare(currentLocalVersion, options: NSStringCompareOptions.NumericSearch) {
return result == .OrderedDescending
}
return false
}
}
let manager = EAVersionChecker.sharedChecker
manager.versionManifestURL = "https://remote.path/to/version.json"
manager.checkForUpdates { urlToOpen, error in
if let error = error {
println("An error occurred checking for the latest version: \(error.localizedDescription)")
return
}
if let urlToOpen = urlToOpen, rootVC = self.window?.rootViewController {
if UIApplication.sharedApplication().canOpenURL(urlToOpen) {
UIApplication.sharedApplication().openURL(urlToOpen)
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment