Skip to content

Commit

Permalink
Update App Store Review (#26)
Browse files Browse the repository at this point in the history
- Update app store review logics

Co-authored-by: James Layton <>
  • Loading branch information
RedDragonJ authored Aug 6, 2024
1 parent 335f3ef commit 09f6f58
Showing 1 changed file with 29 additions and 13 deletions.
42 changes: 29 additions & 13 deletions Sources/JimUtilitySDK/AppReviewManager/AppReviewManager.swift
Original file line number Diff line number Diff line change
@@ -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)
}
}
}

0 comments on commit 09f6f58

Please sign in to comment.