Skip to content

Commit

Permalink
Update App Review Logic (#29)
Browse files Browse the repository at this point in the history
- Update the logic to better align Apple guidlines

Co-authored-by: James Layton <>
  • Loading branch information
RedDragonJ authored Aug 6, 2024
1 parent 550a0cf commit 4b3096a
Showing 1 changed file with 25 additions and 38 deletions.
63 changes: 25 additions & 38 deletions Sources/JimUtilitySDK/AppReviewManager/AppReviewManager.swift
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,9 @@ import StoreKit
import UIKit

public class AppReviewManager {
private static let lastReviewPromptDateKey = "LastReviewPromptDate"
private static let hasReviewedKey = "HasReviewed"
private static let hasLaunchedKey = "HasLaunched"
private static let reviewPromptInterval: TimeInterval = 72 * 60 * 60 // 72 hours in seconds
private static let visitCountKey = "VisitCount"
private static let reviewPromptThreshold = 5 // Number of visits before prompting for review

// FOR UIKit
/// Checks if the app review request should be presented to the user based on the review status and time interval.
Expand All @@ -26,26 +25,21 @@ public class AppReviewManager {
return
}

let hasReviewed = userDefaults.bool(forKey: hasReviewedKey)
let lastPromptDate = (userDefaults.object(forKey: lastReviewPromptDateKey) as? Date) ?? Date.distantPast
let currentDate = Date()
var visitCount = userDefaults.integer(forKey: visitCountKey)

// 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
}
// Increment the visit count
visitCount += 1
userDefaults.set(visitCount, forKey: visitCountKey)

// 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)
// Check if the visit count has reached the threshold
if visitCount > reviewPromptThreshold {
// Reset the visit count after prompting for review
userDefaults.set(0, forKey: visitCountKey)

// Prompt for review if criteria are met
if let scene = UIApplication.shared.connectedScenes.first(where: { $0.activationState == .foregroundActive }) as? UIWindowScene {
SKStoreReviewController.requestReview(in: scene)
}
}
}

Expand All @@ -61,26 +55,19 @@ public class AppReviewManager {
return
}

let hasReviewed = userDefaults.bool(forKey: hasReviewedKey)
let lastPromptDate = (userDefaults.object(forKey: lastReviewPromptDateKey) as? Date) ?? Date.distantPast
let currentDate = Date()
var visitCount = userDefaults.integer(forKey: visitCountKey)

// Check if the user has already reviewed the app
if hasReviewed {
completion(false)
return
}
// Increment the visit count
visitCount += 1
userDefaults.set(visitCount, forKey: visitCountKey)

// Check if it's been 72 hours since the last review prompt
let timeSinceLastPrompt = currentDate.timeIntervalSince(lastPromptDate)
if timeSinceLastPrompt < reviewPromptInterval {
// Check if the visit count has reached the threshold
if visitCount > reviewPromptThreshold {
// Reset the visit count after prompting for review
userDefaults.set(0, forKey: visitCountKey)
completion(true)
} else {
completion(false)
return
}

// Update user defaults and call completion to request a review
userDefaults.set(true, forKey: hasReviewedKey)
userDefaults.set(currentDate, forKey: lastReviewPromptDateKey)
completion(true)
}
}

0 comments on commit 4b3096a

Please sign in to comment.