Error when requesting review using SKStoreReviewController

291 Views Asked by At

When I use SKStoreReviewController.requestReview() to request a review from the Scene Delegate in my SwiftUI application, I get the following message: Error in UIKit client: -[UIWindow setScreen:] should not be called if the client adopts UIScene lifecycle. Call -[UIWindow setWindowScene:] instead..

I am using the following code in the scene delegate to request the review:

if let windowScene = scene as? UIWindowScene {
    if UserDefaults.standard.integer(forKey: "launchCount") == 10 {
        let window = UIWindow(windowScene: windowScene)
        window.rootViewController = UIHostingController(rootView: homeView)
        self.window = window
        window.makeKeyAndVisible()
        if #available(iOS 14, *) {
            SKStoreReviewController.requestReview(in: windowScene)
        }
        else {
            SKStoreReviewController.requestReview()
        }
    }
}

This code is inside the scene(_ scene: UIScene, willConnectTo session: UISceneSession, options connectionOptions: UIScene.ConnectionOptions) method of the Scene Delegate. The method for iOS 14 is working where the review is being requested from the UIWindowScene, but the method for earlier versions is not working and returns the message specified previously. Is there any way to fix this? Thanks in advance for all the help.

1

There are 1 best solutions below

0
user2168735 On

You can use my small wrapper around SKStoreReviewController which solves this problem.

import SwiftUI
import AppReview

struct ContentView: View {
    var body: some View {
        VStack {
            Text("SwiftUI")
        }.onAppear {
            AppReview.requestIf(launches: 3)
        }
    }
}

https://github.com/mezhevikin/AppReview