Created
July 28, 2018 21:18
-
-
Save atanasovdejan/9f0225820c6370c942766bbfeccd8a1f to your computer and use it in GitHub Desktop.
trigger rating and review screen using SKStoreReviewController
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// | |
// 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