This repository has been archived by the owner on Dec 28, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathReviewHandler.swift
77 lines (68 loc) · 2.96 KB
/
ReviewHandler.swift
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
//
// ReviewHandler.swift
// HSRPizzaHelper
//
// Created by Bill Haku on 2023/5/5.
// 用于弹出App Store评分弹窗
import Defaults
import DefaultsKeys
import Foundation
import StoreKit
import SwiftUI
class ReviewHandler {
// MARK: Lifecycle
// static func requestReview() {
// DispatchQueue.main.async {
// if let scene = UIApplication.shared.connectedScenes
// .first(where: { $0.activationState == .foregroundActive
// }) as? UIWindowScene {
// SKStoreReviewController.requestReview(in: scene)
// }
// }
// }
private init() {}
// MARK: Internal
static func requestReview() {
#if DEBUG
Defaults[.lastVersionPromptedForReview] = nil
#endif
DispatchQueue.main.async {
// Keep track of the most recent app version that prompts the user for a review.
let lastVersionPromptedForReview = Defaults[.lastVersionPromptedForReview]
// Get the current bundle version for the app.
let infoDictionaryKey: String = kCFBundleVersionKey as String
let currentVersion = Bundle.main.object(forInfoDictionaryKey: infoDictionaryKey)
guard let currentVersion = currentVersion as? String else {
fatalError("Expected to find a bundle version in the info dictionary.")
}
// Verify the user completes the process several times and doesn’t receive a prompt for this app version.
if currentVersion != lastVersionPromptedForReview {
if let windowScene = getCurrentUIWindowScene() {
SKStoreReviewController.requestReview(in: windowScene)
Defaults[.lastVersionPromptedForReview] = currentVersion
}
}
}
}
static func requestReviewIfNotRequestedElseNavigateToAppStore() {
let lastVersionPromptedForReview = Defaults[.lastVersionPromptedForReview]
let infoDictionaryKey = kCFBundleVersionKey as String
guard let currentVersion = Bundle.main
.object(forInfoDictionaryKey: infoDictionaryKey) as? String
else { fatalError("Expected to find a bundle version in the info dictionary.") }
// Verify the user completes the process several times and doesn’t receive a prompt for this app version.
if currentVersion != lastVersionPromptedForReview {
ReviewHandler.requestReview()
} else {
guard let writeReviewURL =
URL(string: "https://apps.apple.com/app/id6448894222?action=write-review")
else { fatalError("Expected a valid URL") }
UIApplication.shared.open(writeReviewURL, options: [:], completionHandler: nil)
}
}
// MARK: Private
private static func getCurrentUIWindowScene() -> UIWindowScene? {
UIApplication.shared.connectedScenes
.first(where: { $0.activationState == .foregroundActive }) as? UIWindowScene
}
}