Skip to content

Instantly share code, notes, and snippets.

@luthviar
Last active August 26, 2025 04:28
Show Gist options
  • Select an option

  • Save luthviar/79e1200d5e940505df17f63c9d8ca4c9 to your computer and use it in GitHub Desktop.

Select an option

Save luthviar/79e1200d5e940505df17f63c9d8ca4c9 to your computer and use it in GitHub Desktop.
RequestReviewSwiftUI.swift
import SwiftUI
import StoreKit
struct ContentView: View {
@State private var counter: Int = 0
var body: some View {
VStack(spacing: 20) {
Text("Hello, World!")
.font(.largeTitle)
Text("Counter: \(counter)")
.font(.title)
HStack(spacing: 40) {
Button(action: {
counter -= 1
requestAppStoreReview()
}) {
Text("−")
.font(.largeTitle)
.frame(width: 60, height: 60)
.background(Color.red.opacity(0.2))
.clipShape(Circle())
}
Button(action: {
counter += 1
}) {
Text("+")
.font(.largeTitle)
.frame(width: 60, height: 60)
.background(Color.green.opacity(0.2))
.clipShape(Circle())
}
}
}
.padding()
.setRequestReviewSdk()
}
// MARK: - Helper Methods
private func requestAppStoreReview() {
RequestReviewHelper.shared.triggerReview()
}
}
// MARK: - Helper for RequestReview
class RequestReviewHelper: ObservableObject {
static let shared = RequestReviewHelper()
private var reviewAction: (() -> Void)?
func setReviewAction(_ action: @escaping () -> Void) {
self.reviewAction = action
}
func triggerReview() {
reviewAction?()
}
}
// MARK: - View Modifier for RequestReview Environment
@available(iOS 16.0, *)
struct RequestReviewEnvironmentModifier: ViewModifier {
@Environment(\.requestReview) private var requestReview
func body(content: Content) -> some View {
content
.onAppear {
RequestReviewHelper.shared.setReviewAction {
requestReview()
}
}
}
}
struct RequestReviewEarlierModifier: ViewModifier {
func body(content: Content) -> some View {
content
.onAppear {
RequestReviewHelper.shared.setReviewAction {
SKStoreReviewController.requestReview()
}
}
}
}
extension View {
func setRequestReviewSdk() -> some View {
if #available(iOS 16.0, *) {
return AnyView(modifier(RequestReviewEnvironmentModifier()))
} else {
return AnyView(modifier(RequestReviewEarlierModifier()))
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment