-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Update app store review logics Co-authored-by: James Layton <>
- Loading branch information
1 parent
335f3ef
commit 09f6f58
Showing
1 changed file
with
29 additions
and
13 deletions.
There are no files selected for viewing
42 changes: 29 additions & 13 deletions
42
Sources/JimUtilitySDK/AppReviewManager/AppReviewManager.swift
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} | ||
} | ||
} |