This repository was archived by the owner on Jun 9, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathSoftwareUpdateManager.swift
More file actions
80 lines (63 loc) · 3.08 KB
/
SoftwareUpdateManager.swift
File metadata and controls
80 lines (63 loc) · 3.08 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
//
// SoftwareUpdateManager.swift
//
// Created by William Henderson on 1/9/15.
// Copyright (c) 2015 Knock Softwae, Inc. All rights reserved.
//
import Foundation
class SoftwareUpdateManager : NSObject, UIAlertViewDelegate {
let bucketName = "YOURBUCKETNAME"
let appName = "YourAppName"
let manifestUrl = NSURL(string: "https://s3-us-west-2.amazonaws.com/" + bucketName + "/manifest.plist")!
let minimumUpdateCheckInterval : NSTimeInterval = 60*60*4 // 4 hours
var lastUpdateCheck : NSDate?
struct Static {
static var sharedManager : SoftwareUpdateManager?
}
class var sharedManager:SoftwareUpdateManager {
return Static.sharedManager!
}
class func startup() {
Static.sharedManager = SoftwareUpdateManager()
Static.sharedManager?.startup()
}
func startup() {
NSNotificationCenter.defaultCenter().addObserver(self, selector: "appDidBecomeActive", name: UIApplicationDidBecomeActiveNotification, object: nil)
self.checkForUpdateIfNeeded()
}
deinit {
NSNotificationCenter.defaultCenter().removeObserver(self)
}
func appDidBecomeActive() {
self.checkForUpdateIfNeeded()
}
func checkForUpdateIfNeeded() {
if (self.lastUpdateCheck != nil && abs(self.lastUpdateCheck!.timeIntervalSinceNow) < self.minimumUpdateCheckInterval ) {
return
}
self.lastUpdateCheck = NSDate()
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_BACKGROUND, 0), { () -> Void in
let manifestDictionary = NSDictionary(contentsOfURL: self.manifestUrl) as! [String:AnyObject]?
if (manifestDictionary != nil) {
let items = manifestDictionary?["items"] as! [AnyObject]?
let item = items?.last as! [String:AnyObject]?
let version = item?["metadata"]?["bundle-version"] as! String
let currentVersion = NSBundle.mainBundle().infoDictionary?["CFBundleVersion"] as! String
if (currentVersion.compare(version, options: NSStringCompareOptions.NumericSearch) == NSComparisonResult.OrderedAscending) {
// update is available
if (UIApplication.sharedApplication().applicationState != UIApplicationState.Active) {
let notif = UILocalNotification()
notif.alertBody = "An update to " + appName + " is available! Open " + appName + " to upgrade."
UIApplication.sharedApplication().presentLocalNotificationNow(notif)
}
let alert = UIAlertView(title: appName + " Update Available", message: "", delegate: self, cancelButtonTitle: nil, otherButtonTitles: "Update")
alert.show()
}
}
})
}
func alertView(alertView: UIAlertView, didDismissWithButtonIndex buttonIndex: Int) {
let itmsURL = String(format: "itms-services://?action=download-manifest&url=%@", self.manifestUrl)
UIApplication.sharedApplication().openURL(NSURL(string: itmsURL as String)!)
}
}