diff --git a/Sparkle/SPUStandardUpdaterController.h b/Sparkle/SPUStandardUpdaterController.h index bcc0bf52..2974973e 100644 --- a/Sparkle/SPUStandardUpdaterController.h +++ b/Sparkle/SPUStandardUpdaterController.h @@ -26,59 +26,79 @@ NS_ASSUME_NONNULL_BEGIN /*! A controller class that instantiates a SPUUpdater and allows binding UI to it. - This class can be instantiated in a nib or created using initWithUpdaterDelegate:userDriverDelegate:. The controller's updater targets the application's main bundle - and uses Sparkle's standard user interface. Typically, this class is used by sticking it as a custom NSObject subclass in an Interface Builder nib (probably in MainMenu). + This class can be instantiated in a nib or created programatically using initWithUpdaterDelegate:userDriverDelegate: or initWithStartingUpdater:updaterDelegate:userDriverDelegate:. + + The controller's updater targets the application's main bundle and uses Sparkle's standard user interface. + Typically, this class is used by sticking it as a custom NSObject subclass in an Interface Builder nib (probably in MainMenu) but it works well programatically too. The controller creates an SPUUpdater instance and allows hooking up the check for updates action and menu item validation. It also allows hooking up the updater's and user driver's delegates. - This controller class may not be valuable to you if: - * You want to control or defer starting the SPUUpdater, or don't want to be tied into a nib's instantiation, or don't want to use a nib - * You want to target a bundle that's not the main bundle - * You want to provide a custom user interface (SPUUserDriver), or perhaps one that provides little-to-none + If you need more control over what bundle you want to update or you want to provide a custom user interface (via SPUUserDriver), please use SPUUpdater directly instead. */ SU_EXPORT @interface SPUStandardUpdaterController : NSObject /*! Interface builder outlet for the updater's delegate. - This property should only be set using Interface Builder by creating a connection using the outlet + This property should only be set using Interface Builder by creating a connection using the outlet. */ @property (nonatomic, weak, nullable) IBOutlet id updaterDelegate; /*! Interface builder outlet for the user driver's delegate. - This property should only be set using Interface Builder by creating a connection using the outlet + This property should only be set using Interface Builder by creating a connection using the outlet. */ @property (nonatomic, weak, nullable) IBOutlet id userDriverDelegate; /*! Accessible property for the updater. Some properties on the updater can be binded via KVO - This is nil before being loaded from the nib. - You may access this property after your application has finished launching, or after your window controller has finished loading. + When instantiated from a nib, don't perform update checks before the application has finished launching in a MainMenu nib (i.e applicationDidFinishLaunching:) or before the corresponding window/view controller has been loaded (i.e, windowDidLoad or viewDidLoad). The updater is not guaranteed to be started yet before these points. */ -@property (nonatomic, readonly, nullable) SPUUpdater *updater; +@property (nonatomic, readonly) SPUUpdater *updater; /*! Accessible property for the updater's user driver. - - This is nil before being loaded from the nib. - You may access this property after your application has finished launching, or after your window controller has finished loading. */ -@property (nonatomic, readonly, nullable) SPUStandardUserDriver *userDriver; +@property (nonatomic, readonly) SPUStandardUserDriver *userDriver; /*! - Use initWithUpdaterDelegate:userDriverDelegate: instead. + Create a new SPUStandardUpdaterController from a nib. + + You cannot call this initializer directly. You must instantiate a SPUStandardUpdaterController inside of a nib (typically the MainMenu nib) to use it. + + To create a SPUStandardUpdaterController programatically, use initWithUpdaterDelegate:userDriverDelegate: or initWithStartingUpdater:updaterDelegate:userDriverDelegate: instead. */ - (instancetype)init NS_UNAVAILABLE; /*! Create a new SPUStandardUpdaterController programmatically. + + The updater is started automatically. See -[SPUStandardUpdaterController startUpdater] for more information. */ - (instancetype)initWithUpdaterDelegate:(nullable id)updaterDelegate userDriverDelegate:(nullable id)userDriverDelegate; +/*! + Create a new SPUStandardUpdaterController programmatically allowing you to specify whether or not to start the updater immediately. + + You can specify whether or not you want to start the updater immediately. If you do not start the updater, you + must invoke -[SPUStandardUpdaterController startUpdater] at a later time to start it. + */ +- (instancetype)initWithStartingUpdater:(BOOL)startUpdater updaterDelegate:(nullable id)updaterDelegate userDriverDelegate:(nullable id)userDriverDelegate; + +/*! + Starts the updater if it has not already been started. + + You should only call this method yourself if you opted out of starting the updater on initialization. + Hence, do not call this yourself if you are instantiating this controller from a nib. + + This invokes -[SPUUpdater startUpdater:]. If the application is misconfigured with Sparkle, an error is logged and an alert is shown to the user (after a few seconds) to contact the developer. + If you want more control over this behavior, you can create your own SPUUpdater instead. + */ +- (void)startUpdater; + /*! Explicitly checks for updates and displays a progress dialog while doing so. @@ -89,7 +109,7 @@ SU_EXPORT @interface SPUStandardUpdaterController : NSObject This action checks updates by invoking -[SPUUpdater checkForUpdates] */ -- (IBAction)checkForUpdates:(id)sender; +- (IBAction)checkForUpdates:(nullable id)sender; /*! Validates if the menu item for checkForUpdates: can be invoked or not diff --git a/Sparkle/SPUStandardUpdaterController.m b/Sparkle/SPUStandardUpdaterController.m index 03488d51..7d137e32 100644 --- a/Sparkle/SPUStandardUpdaterController.m +++ b/Sparkle/SPUStandardUpdaterController.m @@ -17,57 +17,53 @@ #import "SULocalizations.h" #import -static NSString *const SUUpdaterDefaultsObservationContext = @"SUUpdaterDefaultsObservationContext"; - -@interface SPUStandardUpdaterController () - -@property (nonatomic) SPUUpdater *updater; -@property (nonatomic) SPUStandardUserDriver *userDriver; -@property (nonatomic) BOOL initializedUpdater; - -@end - @implementation SPUStandardUpdaterController @synthesize updater = _updater; @synthesize userDriver = _userDriver; @synthesize updaterDelegate = _updaterDelegate; @synthesize userDriverDelegate = _userDriverDelegate; -@synthesize initializedUpdater = _initializedUpdater; + +- (instancetype)init +{ + // Updater will be later started in -awakeFromNib + // Our updater delegate and user driver delegate outlets will be connected by then too. + // For now we can make sure the updater/userDriver are accessible + return [self initWithStartingUpdater:NO updaterDelegate:nil userDriverDelegate:nil]; +} - (void)awakeFromNib { - // awakeFromNib might be called more than once; guard against that + // Note: awakeFromNib might be called more than once but -startUpdate handles that // We have to use awakeFromNib otherwise the delegate outlets may not be connected yet, // and we aren't a proper window or view controller, so we don't have a proper "did load" point - [self initializeUpdater]; + [self startUpdater]; } - (instancetype)initWithUpdaterDelegate:(nullable id)updaterDelegate userDriverDelegate:(nullable id)userDriverDelegate +{ + return [self initWithStartingUpdater:YES updaterDelegate:updaterDelegate userDriverDelegate:userDriverDelegate]; +} + +- (instancetype)initWithStartingUpdater:(BOOL)startUpdater updaterDelegate:(nullable id)updaterDelegate userDriverDelegate:(nullable id)userDriverDelegate { if ((self = [super init])) { _updaterDelegate = updaterDelegate; _userDriverDelegate = userDriverDelegate; - [self initializeUpdater]; + NSBundle *hostBundle = [NSBundle mainBundle]; + SPUStandardUserDriver *userDriver = [[SPUStandardUserDriver alloc] initWithHostBundle:hostBundle delegate:userDriverDelegate]; + + _updater = [[SPUUpdater alloc] initWithHostBundle:hostBundle applicationBundle:hostBundle userDriver:userDriver delegate:updaterDelegate]; + _userDriver = userDriver; + + if (startUpdater) { + [self startUpdater]; + } } return self; } -- (void)initializeUpdater -{ - if (!self.initializedUpdater) { - self.initializedUpdater = YES; - - NSBundle *hostBundle = [NSBundle mainBundle]; - SPUStandardUserDriver *userDriver = [[SPUStandardUserDriver alloc] initWithHostBundle:hostBundle delegate:self.userDriverDelegate]; - self.updater = [[SPUUpdater alloc] initWithHostBundle:hostBundle applicationBundle:hostBundle userDriver:userDriver delegate:self.updaterDelegate]; - self.userDriver = userDriver; - - [self startUpdater]; - } -} - - (void)startUpdater { NSError *updaterError = nil; @@ -84,7 +80,7 @@ static NSString *const SUUpdaterDefaultsObservationContext = @"SUUpdaterDefaults } } -- (IBAction)checkForUpdates:(id)__unused sender +- (IBAction)checkForUpdates:(nullable id)__unused sender { [self.updater checkForUpdates]; } diff --git a/Sparkle/SPUUpdater.m b/Sparkle/SPUUpdater.m index 89a8800c..b6de9480 100644 --- a/Sparkle/SPUUpdater.m +++ b/Sparkle/SPUUpdater.m @@ -144,6 +144,10 @@ NSString *const SUUpdaterAppcastNotificationKey = @"SUUpdaterAppCastNotification - (BOOL)startUpdater:(NSError * __autoreleasing *)error { + if (self.startedUpdater) { + return YES; + } + if (![self checkIfConfiguredProperlyAndRequireFeedURL:NO error:error]) { return NO; } diff --git a/Sparkle/SUUpdater.h b/Sparkle/SUUpdater.h index f173c4b6..9116e862 100644 --- a/Sparkle/SUUpdater.h +++ b/Sparkle/SUUpdater.h @@ -34,7 +34,7 @@ Note: This class is now deprecated and acts as a thin wrapper around SPUUpdater and SPUStandardUserDriver */ -__deprecated_msg("Deprecated in Sparkle 2. Use SPUStandardUpdaterController or SPUUpdater instead") +__deprecated_msg("Deprecated in Sparkle 2. Use SPUStandardUpdaterController instead, or SPUUpdater if you need more control.") SU_EXPORT @interface SUUpdater : NSObject @property (unsafe_unretained, nonatomic) IBOutlet id delegate; diff --git a/Sparkle/SUUpdater.m b/Sparkle/SUUpdater.m index e9e06198..63200aa3 100644 --- a/Sparkle/SUUpdater.m +++ b/Sparkle/SUUpdater.m @@ -94,7 +94,6 @@ static NSMutableDictionary *sharedUpdaters = nil; // This will be used when the updater is instantiated in a nib such as MainMenu - (instancetype)init { - SULog(SULogLevelDefault, @"DEPRECATION: SUUpdater is deprecated in Sparkle 2 but functional for transitional purposes. Please migrate to SPUStandardUpdaterController as a nib instantiated replacement, or SPUUpdater."); return [self initForBundle:[NSBundle mainBundle]]; }