Quick tip on how to fix the error which mostly depends on where you are calling the code in your app.
In my app for example I use a similar code in two places. Common among them is it tries to determine the first key window in the application regardless of the scene.
I have this function that I check from the list of UIApplication.shared.windows and filter by the top .isKeyWindow window so I can retrieve the main rootViewController to navigate to that view in Swift.
It uses the windows object which has been deprecated in IOS 15.
An improved way is to use UIWindowScene. You first determine the app’s scenes. Then you can traverse the UIWindowScene and grab the controller like so.
This code above works well.
Another code I had to refactor was the one that calls SKStoreViewController to request user reviews.
Hope you like this tip, hit the comments below for any questions.