引导用户为 App 评分
2023-12-30 本文已影响0人
_浅墨_
showing the review alert 核心代码:
import SwiftUI
import StoreKit
enum ReviewRequest {
// Change this to how many times you want the function to be called before showing the review alert
static var limit = 10
@AppStorage("runsSinceLastRequest") static var runsSinceLastRequest = 0
@AppStorage("version") static var version = ""
static func showReview() {
runsSinceLastRequest += 1
// print("runsSinceLastRequest", runsSinceLastRequest)
let appBuild = Bundle.main.object(forInfoDictionaryKey: "CFBundleVersion") as! String // To get the app build number
let appVersion = Bundle.main.object(forInfoDictionaryKey: "CFBundleShortVersionString") as! String // To get the app build version
let currentVersion = "Version \(appVersion), build \(appBuild)"
guard currentVersion != version else { return }
guard runsSinceLastRequest == limit else { return }
if let scene = UIApplication.shared.connectedScenes.first(where: {$0.activationState == .foregroundActive}) as? UIWindowScene {
SKStoreReviewController.requestReview(in: scene)
runsSinceLastRequest = 0
version = currentVersion
}
}
}
使用方法,比如在首页调用:
override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(animated)
// 用户点击几次后,引导他去评分
ReviewRequest.showReview()
}