You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

69 lines
2.2 KiB

// Copyright © 2015 Abhishek Banthia
import Cocoa
import StoreKit
final class ReviewController {
private static var storage = UserDefaults.standard
private static var debugging = false
private enum Keys {
static let lastPrompt = "last-prompt"
static let lastVersion = "last-version"
static let install = "install"
}
3 years ago
class func applicationDidLaunch(_ defaults: UserDefaults) {
if ProcessInfo.processInfo.arguments.contains(UserDefaultKeys.testingLaunchArgument) {
debugging = true
}
storage = defaults
3 years ago
if defaults.object(forKey: Keys.install) == nil {
defaults.set(Date(), forKey: Keys.install)
}
}
class func setPreviewMode(_ value: Bool) {
debugging = value
}
class func prompted() {
storage.set(Date(), forKey: Keys.lastPrompt)
storage.set(Bundle.main.object(forInfoDictionaryKey: "CFBundleVersion") as? String, forKey: Keys.lastVersion)
}
class func canPrompt() -> Bool {
3 years ago
if debugging == true {
return true
}
let day: TimeInterval = -1 * 60 * 60 * 24
let minInstall: TimeInterval = day * 7
// Check if the app has been installed for atleast 7 days
guard let install = storage.object(forKey: Keys.install) as? Date,
3 years ago
install.timeIntervalSinceNow < minInstall
6 years ago
else { return false }
// If we have never been prompted before, go ahead and prompt
guard let lastPrompt = storage.object(forKey: Keys.lastPrompt) as? Date,
3 years ago
let lastVersion = storage.object(forKey: Keys.lastVersion) as? String
6 years ago
else { return true }
3 years ago
// Minimum interval between two versions should be 3 months
let minInterval: TimeInterval = day * 90
// never prompt w/in the same version
return lastVersion != (Bundle.main.object(forInfoDictionaryKey: "CFBundleVersion") as? String)
// limit all types of prompts to at least 1mo intervals
&& lastPrompt.timeIntervalSinceNow < minInterval
}
class func prompt() {
SKStoreReviewController.requestReview()
prompted()
}
}