@@ -5,15 +5,25 @@ import Sparkle
55public final class UpdateChecker {
66 let updater : SPUUpdater
77 let hostBundleFound : Bool
8- let delegate = UpdaterDelegate ( )
8+ let delegate : UpdaterDelegate
9+ public weak var updateCheckerDelegate : UpdateCheckerDelegate ? {
10+ get { delegate. updateCheckerDelegate }
11+ set { delegate. updateCheckerDelegate = newValue }
12+ }
913
10- public init ( hostBundle: Bundle ? ) {
14+ public init (
15+ hostBundle: Bundle ? ,
16+ shouldAutomaticallyCheckForUpdate: Bool
17+ ) {
1118 if hostBundle == nil {
1219 hostBundleFound = false
1320 Logger . updateChecker. error ( " Host bundle not found " )
1421 } else {
1522 hostBundleFound = true
1623 }
24+ delegate = . init(
25+ shouldAutomaticallyCheckForUpdate: shouldAutomaticallyCheckForUpdate
26+ )
1727 updater = SPUUpdater (
1828 hostBundle: hostBundle ?? Bundle . main,
1929 applicationBundle: Bundle . main,
@@ -37,7 +47,32 @@ public final class UpdateChecker {
3747 }
3848}
3949
50+ public protocol UpdateCheckerDelegate : AnyObject {
51+ func prepareForRelaunch( finish: @escaping ( ) -> Void )
52+ }
53+
4054class UpdaterDelegate : NSObject , SPUUpdaterDelegate {
55+ let shouldAutomaticallyCheckForUpdate : Bool
56+ weak var updateCheckerDelegate : UpdateCheckerDelegate ?
57+
58+ init ( shouldAutomaticallyCheckForUpdate: Bool ) {
59+ self . shouldAutomaticallyCheckForUpdate = shouldAutomaticallyCheckForUpdate
60+ }
61+
62+ func updater( _ updater: SPUUpdater , mayPerform updateCheck: SPUUpdateCheck ) throws {
63+ if !shouldAutomaticallyCheckForUpdate, updateCheck == . updatesInBackground {
64+ throw CancellationError ( )
65+ }
66+ }
67+
68+ func updater( _ updater: SPUUpdater , shouldPostponeRelaunchForUpdate item: SUAppcastItem , untilInvokingBlock installHandler: @escaping ( ) -> Void ) -> Bool {
69+ if let updateCheckerDelegate {
70+ updateCheckerDelegate. prepareForRelaunch ( finish: installHandler)
71+ return true
72+ }
73+ return false
74+ }
75+
4176 func allowedChannels( for updater: SPUUpdater ) -> Set < String > {
4277 if UserDefaults . shared. value ( for: \. installBetaBuilds) {
4378 Set ( [ " beta " ] )
0 commit comments