Apple says
Before we get started writing code, let’s go over some best practices from Apple.
When wanting to request a review from a user, Apple suggests that you:
- Wait for a positive interaction before requesting a review
- Not interrupt the user in the middle of a process
- Not ask the user for a review more than once in the same version of the app
- Not to pester the user with multiple feedback requests
Things to watch out for
When using Apples StoreKit library to request a review there is a rate limit of only 3 requests in a 365 day period. Apple has said that we as developers are required to implement our own rate limiting. This means that if we put the code to request feedback from a user somewhere that could get called frequently, we could use up our 3 requests in one day, and not be able to request a review from the user for another year.
More info from Apples Human Interface Guidelines on requesting reviews can be found here: https://developer.apple.com/design/human-interface-guidelines/ratings-and-reviews
A code example from Apple can be found here: https://developer.apple.com/documentation/storekit/requesting_app_store_reviews
Requesting a review using SwiftUI
In order to request a review from the user for our app, we will only need a few things.
We need to import StoreKit, use the requestReview environment variable, and call the requestReview method.
Here is what that looks like in code:
import SwiftUI
import StoreKit
struct ContentView: View {
@Environment(\.requestReview) var requestReview
var body: some View {
Button("Leave a review") {
requestReview()
}
}
}
Once the user clicks the button, a review prompt (similar to the one in the image above) should appear on their screen. Note: there are some issues using this feature in the simulator, but it seemed to work well when using the preview window.
If you got value from this article, please consider following me to get more great Swift content in the future.
If you have any questions on the topic, or know of another way to accomplish the same task, feel free to respond to the post or share it with a friend and get their opinion on it.
If you want to learn more about Swift and SwiftUI, you can check out the other articles I have written here: https://medium.com/@jpmtech
If you want to see apps that have been built with Swift and SwiftUI, you can check out my apps here: https://jpmtech.io/apps
Thank you for taking the time to make it this far. Have a great day!