Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

'requestReview()' was deprecated in iOS 14.0

In iOS 14, Xcode is showing a warning:

requestReview()' was deprecated in iOS 14.0

I'm using StoreKit to ask review automatically in my app.

func requestReview() {     guard shouldRequestReview else {return}     SKStoreReviewController.requestReview()     lastRequest = Date() } 

enter image description here

How to get rid of that warning?

like image 851
Dc7 Avatar asked Sep 18 '20 10:09

Dc7


2 Answers

Quick solution

if let scene = UIApplication.shared.connectedScenes.first(where: { $0.activationState == .foregroundActive }) as? UIWindowScene {     SKStoreReviewController.requestReview(in: scene) } 

Convenient solution

Here's a true one-liner:

SKStoreReviewController.requestReviewInCurrentScene() 

but first you need to create the following extension in SKStoreReviewController:

extension SKStoreReviewController {     public static func requestReviewInCurrentScene() {         if let scene = UIApplication.shared.connectedScenes.first(where: { $0.activationState == .foregroundActive }) as? UIWindowScene {             requestReview(in: scene)         }     } } 

Here is a GitHub repository with different Swift extensions including requestReviewInCurrentScene().


Explanation

The requestReview function was deprecated in iOS 14:

@available(iOS, introduced: 10.3, deprecated: 14.0) open class func requestReview() 

You need to use the requestReview(in:) function instead:

@available(iOS 14.0, *) open class func requestReview(in windowScene: UIWindowScene) 

Possible solutions

  • Custom extension

You can create the following extension:

extension UIApplication {     var currentScene: UIWindowScene? {         connectedScenes             .first { $0.activationState == .foregroundActive } as? UIWindowScene     } } 

and use it like this:

if let scene = UIApplication.shared.currentScene {     SKStoreReviewController.requestReview(in: scene) } 
  • Universal one-liner:
if let scene = UIApplication.shared.connectedScenes.first(where: { $0.activationState == .foregroundActive }) as? UIWindowScene {     SKStoreReviewController.requestReview(in: scene) } 
  • single scene solution (for iOS)
if let scene = UIApplication.shared.connectedScenes.first as? UIWindowScene {     SKStoreReviewController.requestReview(in: scene) } 
like image 159
pawello2222 Avatar answered Sep 25 '22 15:09

pawello2222


Simply solution for iOS 13 and above

Swift 5+

    if #available(iOS 14.0, *) {         if let scene = UIApplication.shared.connectedScenes.first(where: { $0.activationState == .foregroundActive }) as? UIWindowScene {             SKStoreReviewController.requestReview(in: scene)         }     } else if #available(iOS 10.3, *) {         SKStoreReviewController.requestReview()     } 
like image 30
Ashvin A Avatar answered Sep 21 '22 15:09

Ashvin A