How to use SKStoreReviewController tutorial — Programming With Swift

Darren
Programming with Swift
5 min readAug 30, 2018

Checkout my other posts over at programmingwithswift.com

In this tutorial I am going to show you how to implement SKStoreReviewController in your app. This won’t be a long tutorial as it is quite simple to implement, so lets get started.

When using this API keep in mind that the review controller will only show up 3 times in a 365 day period. So in order to get the maximum potential out of it you will need to choose carefully where to place it and when to show it. Also, you only want to prompt the user once for each version of your app.

According to Apple’s docs, they say that one should try and delay prompting the user for review. This allows the more experienced users of the app to rate your app.

If you have an app already and you have analytics on the app, try and make use of them to see where the best place in the app would be to launch the prompt the user to rate your app. Otherwise I would suggest making it show up after a certain number of times. Just make sure that when you prompt the user, that they are not busy with something, try and ask the user for a rating when they have finished completing a task in your app.

How to implement SKStoreReviewController

Now we can get to the code. I am going to show you the code and then go through it afterwards.

What will the code do? We will create a struct that will help with taking care of the logic that is needed for SKStoreReviewController.

This struct will contain 3 things:

  1. Key, numberOfTimesLaunchedKey, to that will be used in userDefaults to store the number of times the app has been opened.
  2. A function called displayStoreKit which will do all the checks to make sure that we can display the SKStoreReviewController based on the rules that we give it.
  3. The last thing that this struct has is a function that allows us to increment the number of times that the app has been opened. This will make use of the numberOfTimesLaunchedKey in order to update that value that is stored in userDefaults.

Step 1: Import StoreKit

The first thing that you need to do is to import StoreKit. Once you have done that, create a struct called StoreKitHelper.

Step 2: Create the userDefaults key

You now need to create a new static let called numberOfTimesLaunchedKey, this will be used to read and write to the userDefaults so that we can keep track of how many times the app has been opened.

Step 3: Create a the function to display SKStoreReviewController

You now need to create a new static function called displayStoreKit. This function will do everything in order to show the SKStoreReviewController.

What is happening in the the displayStoreKit function?

The first thing that we do in this function is to get the current version of the app, I created a new variable called, currentVersion, that we will use to store that value. I used a guard let that returns nothing because if we cannot get the version number then we should not carry on with the rest of this function.

Next I created another let variable, lastVersionPromptedForReview, this will get the last version that showed the SKStoreReviewController.

After that we need to get the number of times the app is launched. To do that I created another let variable, numberOfTimesLaunched, which will use the numberOfTimesLaunchedKey to access the count of how many times the app has been launched.

Now we create the if statement that will check if all the requirements for showing SKStoreReviewController are met and if so then to show it. In the if statement I just check that the app has been opened more than two times and that the currentVersion of the app is not the same as the last version.

If all of that is true then it will enter the if statement and prompt the user for a review. Remember this API is only available on iOS 10.3 and up, if you don’t add the check in for the iOS version to see if the API is available, you will not be able to build the app.

Step 4: Increment the number of times the app was opened

Create a new function called, incrementNumberOfTimesLaunched. In this function we will increment the number of times the app has been launched.

To do this, I used the numberOfTimesLaunchedKey variable to get the value that has been stored. Once it has that value it will increment that by 1 and then we will set the value for the numberOfTimesLaunchedKey to be the new incremented value.

Step 5: Increment app start up from app delegate

All I do now is I call the incrementNumberOfTimesLaunched function from the didFinishLaunchingWithOptions function in the appDelegate.

Step 6: Show the SKStoreReviewController

To show the SKStoreReviewController all you need to do is call displayStoreKit like this: StoreKitHelper.displayStoreKit()

Note

In this example I have used the number of times the app has been launched as a way to know if I need to show the SKStoreReviewController or not. You can use anything for this, so if you need it to show up whenever a the user triggers a certain event more that a certain number of times then you just need to update the code with different variable names to match whatever it is that you will be keeping track of.

--

--