Skip to content

Instantly share code, notes, and snippets.

@atanasovdejan
Created July 28, 2018 21:18
Show Gist options
  • Save atanasovdejan/9f0225820c6370c942766bbfeccd8a1f to your computer and use it in GitHub Desktop.
Save atanasovdejan/9f0225820c6370c942766bbfeccd8a1f to your computer and use it in GitHub Desktop.
trigger rating and review screen using SKStoreReviewController
//
// RateApp.swift
// https://medium.com/@dejanatanasov
//
import Foundation
import StoreKit
class RateApp {
static let numberOfLaunches = 5 /* set number of app launches */
/* increments the total number of app launches */
class func incrementAppRuns() {
let runs = getRunCounts() + 1
UserDefaults.standard.set(runs, forKey: "RATE_NUMBER_OF_RUNS")
UserDefaults.standard.synchronize()
}
/* fetches the total number of app launches */
class func getRunCounts() -> Int {
if let runs = UserDefaults.standard.value(forKey: "RATE_NUMBER_OF_RUNS") as? Int{
return runs
}
return 0
}
/* checks whether the rating and review screen should be shown or not */
class func canShowReview() -> Bool{
let runs = getRunCounts()
return runs == numberOfLaunches
}
/* request the rating and review screen to be shown to the user (if condition is satisfied) */
class func showReview(){
if #available(iOS 10.3, *) {
if canShowReview() {
SKStoreReviewController.requestReview()
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment