diff --git a/Sources/JimUtilitySDK/AppReviewManager/AppReviewManager.swift b/Sources/JimUtilitySDK/AppReviewManager/AppReviewManager.swift index 099fc82..6ad6563 100644 --- a/Sources/JimUtilitySDK/AppReviewManager/AppReviewManager.swift +++ b/Sources/JimUtilitySDK/AppReviewManager/AppReviewManager.swift @@ -1,26 +1,42 @@ // // AppReviewManager.swift -// +// // // Created by James Layton on 7/23/20. // import Foundation import StoreKit +import UIKit public class AppReviewManager { - /// Checks if the app review request should be presented to the user based on the launch count. - /// - Parameter frequencyCount: The number of app launches after which the review request should be presented. The default value is 3. - public static func checkAppReview(frequencyCount: Int = 3) { - let defaultManager = DefaultManager.shared - let launchCount = defaultManager.getUserInteger(key: "AppLaunchCount") - if launchCount == frequencyCount { - if let scene = UIApplication.shared.connectedScenes.first(where: { $0.activationState == .foregroundActive }) as? UIWindowScene { - SKStoreReviewController.requestReview(in: scene) - } - defaultManager.setUserInteger(value: 0, key: "AppLaunchCount") - } else if launchCount < frequencyCount { - defaultManager.setUserInteger(value: launchCount + 1, key: "AppLaunchCount") + private static let lastReviewPromptDateKey = "LastReviewPromptDate" + private static let hasReviewedKey = "HasReviewed" + private static let reviewPromptInterval: TimeInterval = 72 * 60 * 60 // 72 hours in seconds + + /// Checks if the app review request should be presented to the user based on the review status and time interval. + public static func checkAppReview() { + let userDefaults = UserDefaults.standard + let hasReviewed = userDefaults.bool(forKey: hasReviewedKey) + let lastPromptDate = (userDefaults.object(forKey: lastReviewPromptDateKey) as? Date) ?? Date.distantPast + let currentDate = Date() + + // Check if the user has already reviewed the app + if hasReviewed { + return + } + + // Check if it's been 72 hours since the last review prompt + let timeSinceLastPrompt = currentDate.timeIntervalSince(lastPromptDate) + if timeSinceLastPrompt < reviewPromptInterval { + return + } + + // Prompt for review if criteria are met + if let scene = UIApplication.shared.connectedScenes.first(where: { $0.activationState == .foregroundActive }) as? UIWindowScene { + SKStoreReviewController.requestReview(in: scene) + userDefaults.set(true, forKey: hasReviewedKey) + userDefaults.set(currentDate, forKey: lastReviewPromptDateKey) } } }