Search code examples
iosswiftskstorereviewcontroller

SKStoreReviewController is being displayed the first time user launches the app


In my iOS app I'm using SKStoreReviewController to request users to rate the app. The Apple documentation says to put the code for requesting the "Rate Us" popup anywhere we want, and they will govern when it will be actually displayed. I wrote the following code in the first view of the app:

func requestReview() {
    SKStoreReviewController.requestReview()
}

The problem is that the popup is displayed to my app's users as soon as they first launch the app, which makes no sense. Is there any way to control the popup's appearance and avoid showing it before certain amount of uses of the app?


Solution

  • SKStoreReviewController.requestReview() will display the popup for the first few times (to be exact, for the first 3 times in a year).

    Create a variable that you increment each time in your application delegate's didFinishLaunchingWithOptions method and save it to UserDefaults. After that, you can check whether a user opened the app enough times.

    AppDelegate

    func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
        var appLaunches = UserDefaults.standard.integer(forKey: "appLaunches")
        appLaunches += 1
        UserDefaults.standard.set(appLaunches, forKey: "appLaunches")
    
        return true
    }
    

    The view controller where you want to display the store review controller

    let appLaunches = UserDefaults.standard.integer(forKey: "appLaunches")
    
    if appLaunches >= [enough number of app launches] {
        SKStoreReviewController.requestReview()
    }