A well-designed app review flow is crucial for any iOS application seeking to build trust and improve visibility in the App Store. In this tutorial, we'll delve into the world of StoreKit 2, a modernized framework that simplifies requesting app reviews within your SwiftUI-based project.

What Is StoreKit 2?

StoreKit 2 is the latest evolution of Apple's StoreKit framework, designed to simplify the management of purchases, transactions, and App Store events. This new framework provides a more consistent and type-safe developer experience, making it easier to integrate with SwiftUI.

Understanding the iOS Review System

Before diving into the code, it's essential to grasp how Apple's review system works. The key characteristics include:

  • A native review dialog displayed within your app
  • Users can rate and write reviews without leaving the app
  • The process is controlled by Apple, ensuring a consistent experience for users

Apple's Rules for Requesting Reviews

To avoid App Store rejection, it's crucial to follow Apple's guidelines when requesting reviews. These rules include:

  • Only request reviews using the official API
  • Ask after a positive user interaction
  • Make the request polite and optional

Project Requirements

To get started with StoreKit 2 in SwiftUI, you'll need:

  • Xcode 14 or later (Xcode 15+ recommended)
  • iOS 15 or later
  • A SwiftUI-based project

No special configuration is required in App Store Connect to request reviews.

Importing StoreKit in SwiftUI

In any file where you plan to request a review, import StoreKit:

`swift

import StoreKit

`

With StoreKit 2, the review functionality remains system-based but integrates cleanly with SwiftUI.

The Recommended SwiftUI Approach: `requestReview`

Apple recommends using the SwiftUI environment value to request reviews. Accessing the environment:

`swift

@Environment(\.requestReview) var requestReview

`

This approach is fully compatible with StoreKit 2 and provides a clean solution for SwiftUI apps.

Requesting a Review (Basic Usage)

The simplest way to request a review is to call:

`swift

requestReview()

`

This tells the system that your app would like to show the review dialog—if the system allows it.

Minimal SwiftUI Example

Here's a basic example of requesting a review in SwiftUI:

`swift

import SwiftUI

import StoreKit

struct ContentView: View {

@Environment(\.requestReview) var requestReview

var body: some View {

VStack(spacing: 24) {

Text("Thank you for using our app")

.font(.title)

Button("Rate the app") {

requestReview()

}

}

.padding()

}

}

`

While this works, it's not recommended for production as it doesn't control timing or frequency.

Choosing the Right Moment to Ask for a Review

Timing is everything when asking for reviews. Avoid asking:

  • Immediately after launching the app
  • Before the user has done anything
  • During a critical task

Instead, ask after:

  • Completing a task
  • Reaching a milestone
  • After several successful uses
  • At the end of a positive experience

Controlling Frequency with `UserDefaults`

Although Apple enforces its own limits, it's good practice to implement your own internal control. Here's an example:

`swift

struct ReviewManager {

static func incrementUsage() {

let count = UserDefaults.standard.integer(forKey: "usageCount")

UserDefaults.standard.set(count + 1, forKey: "usageCount")

}

static func shouldRequestReview() -> Bool {

let count = UserDefaults.standard.integer(forKey: "usageCount")

return count == 5 || count == 20

}

}

`

Integrating with SwiftUI and StoreKit 2

Here's an example of integrating the review request with a positive user action:

`swift

struct TaskCompletedView: View {

@Environment(\.requestReview) var requestReview

var body: some View {

Text("Task completed!")

.onAppear {

ReviewManager.incrementUsage()

if ReviewManager.shouldRequestReview() {

requestReview()

}

}

}

}

`

Asking If the User Likes the App First

A common strategy is to filter the experience before requesting a review. Here's an example:

`swift

@State private var showAlert = false

.alert("Are you enjoying the app?", isPresented: $showAlert) {

Button("Yes") {

requestReview()

}

Button("No", role: .cancel) {

// Show feedback form

}

}

`

This improves review quality and reduces negative public feedback.

StoreKit 2 and Window Scenes

In more advanced cases, you may need direct access to the active window scene:

`swift

if let windowScene = SceneDelegate.shared().windowScene {

// Access the active window scene

}

`

By following these best practices and guidelines, you'll be able to create a seamless review request flow in your SwiftUI-based app using StoreKit 2.