Skip to content

Instantly share code, notes, and snippets.

@samuelowino
Created October 23, 2022 07:52
Show Gist options
  • Save samuelowino/26a945e479636ccda85d572fda611b47 to your computer and use it in GitHub Desktop.
Save samuelowino/26a945e479636ccda85d572fda611b47 to your computer and use it in GitHub Desktop.
Request Review
// If the app doesn't store the count, this returns 0.
var count = UserDefaults.standard.integer(forKey: UserDefaultsKeys.processCompletedCountKey)
count += 1
UserDefaults.standard.set(count, forKey: UserDefaultsKeys.processCompletedCountKey)
print("Process completed \(count) time(s).")
// Keep track of the most recent app version that prompts the user for a review.
let lastVersionPromptedForReview = UserDefaults.standard.string(forKey: UserDefaultsKeys.lastVersionPromptedForReviewKey)
// Get the current bundle version for the app.
let infoDictionaryKey = kCFBundleVersionKey as String
guard let currentVersion = Bundle.main.object(forInfoDictionaryKey: infoDictionaryKey) as? String
else { fatalError("Expected to find a bundle version in the info dictionary.") }
// Verify the user completes the process several times and doesn’t receive a prompt for this app version.
if count >= 4 && currentVersion != lastVersionPromptedForReview {
Task { @MainActor [weak self] in
// Delay for two seconds to avoid interrupting the person using the app.
// Use the equation n * 10^9 to convert seconds to nanoseconds.
try? await Task.sleep(nanoseconds: UInt64(2e9))
if let windowScene = self?.view.window?.windowScene,
self?.navigationController?.topViewController is ProcessCompletedViewController {
SKStoreReviewController.requestReview(in: windowScene)
UserDefaults.standard.set(currentVersion, forKey: UserDefaultsKeys.lastVersionPromptedForReviewKey)
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment