Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add a Review Delay Option #5

Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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!

Expand Down
27 changes: 18 additions & 9 deletions Sources/ReviewKit/ReviewKit.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -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
Expand Down