From 305e33945f71e6305d71ddab43898492f7e76637 Mon Sep 17 00:00:00 2001 From: Zorg Date: Sun, 1 May 2016 16:21:22 -0400 Subject: [PATCH] Change update permission prompt to prompt after a few hours The old behavior used to be to prompt on second launch. However, applications can be re-launched in a short amount of time, and Sparkle can be loaded multiple times during a single app launch. So prompting after a certain amount of time after first launch works better. --- Sparkle/SUConstants.h | 3 ++- Sparkle/SUConstants.m | 5 ++++- Sparkle/SUUpdater.m | 26 +++++++++++++++++++------- 3 files changed, 25 insertions(+), 9 deletions(-) diff --git a/Sparkle/SUConstants.h b/Sparkle/SUConstants.h index a25c6dbc..4d482494 100644 --- a/Sparkle/SUConstants.h +++ b/Sparkle/SUConstants.h @@ -16,6 +16,7 @@ // Misc: // ----------------------------------------------------------------------------- +extern const NSTimeInterval SUDefaultUpdatePermissionPromptInterval; extern const NSTimeInterval SUMinimumUpdateCheckInterval; extern const NSTimeInterval SUDefaultUpdateCheckInterval; extern const NSTimeInterval SUImpatientUpdateCheckInterval; @@ -33,7 +34,6 @@ extern NSString *const SUTechnicalErrorInformationKey; // ----------------------------------------------------------------------------- extern NSString *const SUFeedURLKey; -extern NSString *const SUHasLaunchedBeforeKey; extern NSString *const SUShowReleaseNotesKey; extern NSString *const SUSkippedVersionKey; extern NSString *const SUScheduledCheckIntervalKey; @@ -48,6 +48,7 @@ extern NSString *const SUEnableSystemProfilingKey; extern NSString *const SUSendProfileInfoKey; extern NSString *const SULastProfileSubmitDateKey; extern NSString *const SUPromptUserOnFirstLaunchKey; +extern NSString *const SUTimeIntervalAtFirstLaunchKey; extern NSString *const SUKeepDownloadOnFailedInstallKey; extern NSString *const SUDefaultsDomainKey; extern NSString *const SUEnableJavaScriptKey; diff --git a/Sparkle/SUConstants.m b/Sparkle/SUConstants.m index 8df9290c..d3081230 100644 --- a/Sparkle/SUConstants.m +++ b/Sparkle/SUConstants.m @@ -21,6 +21,9 @@ const NSTimeInterval SUMinimumUpdateCheckInterval = DEBUG ? 60 : (60 * 60); const NSTimeInterval SUDefaultUpdateCheckInterval = DEBUG ? 60 : (60 * 60 * 24); +// Define the amount of time between Sparkle being started for the first time and before we can prompt the user for permission about checking for updates automatically +const NSTimeInterval SUDefaultUpdatePermissionPromptInterval = DEBUG ? (60 * 3) : (60 * 60 * 3); + // If the update has already been automatically downloaded, we normally don't want to bug the user about the update // However if the user has gone a very long time without quitting an application, we will bug them // This is the time interval for a "week"; it doesn't matter that this measure is imprecise. @@ -30,7 +33,6 @@ NSString *const SUBundleIdentifier = @SPARKLE_BUNDLE_IDENTIFIER; NSString *const SUTechnicalErrorInformationKey = @"SUTechnicalErrorInformation"; -NSString *const SUHasLaunchedBeforeKey = @"SUHasLaunchedBefore"; NSString *const SUFeedURLKey = @"SUFeedURL"; NSString *const SUShowReleaseNotesKey = @"SUShowReleaseNotes"; NSString *const SUSkippedVersionKey = @"SUSkippedVersion"; @@ -46,6 +48,7 @@ NSString *const SUEnableAutomaticChecksKey = @"SUEnableAutomaticChecks"; NSString *const SUSendProfileInfoKey = @"SUSendProfileInfo"; NSString *const SULastProfileSubmitDateKey = @"SULastProfileSubmissionDate"; NSString *const SUPromptUserOnFirstLaunchKey = @"SUPromptUserOnFirstLaunch"; +NSString *const SUTimeIntervalAtFirstLaunchKey = @"SUTimeIntervalAtFirstLaunch"; NSString *const SUEnableJavaScriptKey = @"SUEnableJavaScript"; NSString *const SUFixedHTMLDisplaySizeKey = @"SUFixedHTMLDisplaySize"; NSString *const SUKeepDownloadOnFailedInstallKey = @"SUKeepDownloadOnFailedInstall"; diff --git a/Sparkle/SUUpdater.m b/Sparkle/SUUpdater.m index 31e9e03b..5d541171 100644 --- a/Sparkle/SUUpdater.m +++ b/Sparkle/SUUpdater.m @@ -216,7 +216,8 @@ NSString *const SUUpdaterAppcastNotificationKey = @"SUUpdaterAppCastNotification - (void)startUpdateCycle { BOOL shouldPrompt = NO; - BOOL hasLaunchedBefore = [self.host boolForUserDefaultsKey:SUHasLaunchedBeforeKey]; + NSNumber *timeIntervalAtFirstLaunch = [self.host objectForUserDefaultsKey:SUTimeIntervalAtFirstLaunchKey]; + NSDate *currentDate = [NSDate date]; // If the user has been asked about automatic checks, don't bother prompting if ([self.host objectForUserDefaultsKey:SUEnableAutomaticChecksKey]) { @@ -230,15 +231,26 @@ NSString *const SUUpdaterAppcastNotificationKey = @"SUUpdaterAppCastNotification else if ([self.delegate respondsToSelector:@selector(updaterShouldPromptForPermissionToCheckForUpdates:)]) { shouldPrompt = [self.delegate updaterShouldPromptForPermissionToCheckForUpdates:self]; } - // Has he been asked already? And don't ask if the host has a default value set in its Info.plist. + // Has the user been asked already? And don't ask if the host has a default value set in its Info.plist. else if ([self.host objectForKey:SUEnableAutomaticChecksKey] == nil) { - // Now, we don't want to ask the user for permission to do a weird thing on the first launch. - // We wait until the second launch, unless explicitly overridden via SUPromptUserOnFirstLaunchKey. - shouldPrompt = [self.host objectForKey:SUPromptUserOnFirstLaunchKey] || hasLaunchedBefore; + // Now, we don't want to ask the user for permission to do a weird thing right when they install the app + // We wait until another launch after a few hours have passed, unless explicitly overridden via SUPromptUserOnFirstLaunchKey. + + if ([self.host objectForKey:SUPromptUserOnFirstLaunchKey] != nil) { + shouldPrompt = YES; + } else if (timeIntervalAtFirstLaunch != nil) { + NSDate *firstLaunchDate = [NSDate dateWithTimeIntervalSinceReferenceDate:timeIntervalAtFirstLaunch.doubleValue]; + NSTimeInterval intervalSinceFirstLaunch = [currentDate timeIntervalSinceDate:firstLaunchDate]; + // We want to prompt if more than (or equal to) 'SUDefaultUpdatePermissionPromptInterval' seconds have passed since the first launch + // If the first launch time is after (or equal to) our current date, then something may have gone wrong in the system - prompt to be on the safe side + if (intervalSinceFirstLaunch >= SUDefaultUpdatePermissionPromptInterval || intervalSinceFirstLaunch <= 0) { + shouldPrompt = YES; + } + } } - if (!hasLaunchedBefore) { - [self.host setBool:YES forUserDefaultsKey:SUHasLaunchedBeforeKey]; + if (timeIntervalAtFirstLaunch == nil) { + [self.host setObject:@([currentDate timeIntervalSinceReferenceDate]) forUserDefaultsKey:SUTimeIntervalAtFirstLaunchKey]; } if (shouldPrompt) {