diff --git a/README.md b/README.md index 814a729..487e2cd 100644 --- a/README.md +++ b/README.md @@ -39,6 +39,11 @@ Read the [Introduction Article](https://www.fline.dev/introducing-reviewkit/?ref ```Swift ReviewKit.recordPositiveEvent() // optionally, you can pass a custom `weight` parameter, defaults to 1 ``` + +5. (Optional) Set a delay between crossing the threshold and showing the review request. + ```swift + ReviewKit.reviewDelay = 5.0 + ``` That's it – you have configured App Review requests for your app! diff --git a/Sources/ReviewKit/ReviewKit.swift b/Sources/ReviewKit/ReviewKit.swift index 24fad70..1b64314 100644 --- a/Sources/ReviewKit/ReviewKit.swift +++ b/Sources/ReviewKit/ReviewKit.swift @@ -9,6 +9,9 @@ public enum ReviewKit { /// Turns off the review request inside an `#if DEBUG`. On by default (for testing purposes). public static var enabledInDebugBuilds: Bool = true + /// The amount of time (in seconds) between the minPositiveEventsWeight threshold being crossed and the review dialog being presented. This can be useful, for example, to prevent the review dialog from interrupting the user during their immediate workflow. + public static var reviewDelay: TimeInterval = 0.0 + /// Records a positive event and requests a review if the criteria are met. Use when a user has completed a workflow and is less likely to be annoyed. /// - Parameter weight: The weight of the positive event. Defaults to 1. public static func recordPositiveEventAndRequestReviewIfCriteriaMet(weight: Int = 1) { @@ -34,19 +37,25 @@ public enum ReviewKit { let totalPositiveEventsWeight = self.positiveEvents.reduce(into: 0, { $0 += $1.weight }) if totalPositiveEventsWeight >= self.criteria.minPositiveEventsWeight { - #if os(iOS) - if - #available(iOS 14.0, *), - let windowScene = UIApplication.shared.connectedScenes.first(where: { $0.activationState == .foregroundActive }) as? UIWindowScene - { - SKStoreReviewController.requestReview(in: windowScene) + if reviewDelay > 0.0 { + DispatchQueue.main.asyncAfter(deadline: .now() + reviewDelay) { presentReview() } } else { - SKStoreReviewController.requestReview() + presentReview() } - #elseif os(macOS) + } + } + + private static func presentReview() { + #if os(iOS) + if #available(iOS 14.0, *), + let windowScene = UIApplication.shared.connectedScenes.first(where: { $0.activationState == .foregroundActive }) as? UIWindowScene { + SKStoreReviewController.requestReview(in: windowScene) + } else { SKStoreReviewController.requestReview() - #endif } + #elseif os(macOS) + SKStoreReviewController.requestReview() + #endif } static var positiveEvents: [PositiveEvent] = UserDefaults.standard