Skip to content

Instantly share code, notes, and snippets.

@dipkasyap
Forked from abhimuralidharan/StoreReviewHelper.swift
Last active December 5, 2019 23:46
Show Gist options
  • Save dipkasyap/63faa43f1a09e6658cdbb3d07071ecec to your computer and use it in GitHub Desktop.
Save dipkasyap/63faa43f1a09e6658cdbb3d07071ecec to your computer and use it in GitHub Desktop.
//
// AppRater.swift
//
// Created by Devi Prasad Ghimire on 6/12/19.
//
import Foundation
import StoreKit
fileprivate struct UserDefaultsKeys {
// Reviews
static let lastVersionPromptedForReviewKey = "LAST_VERSION_PROMOTED_FOR_REVIEW"
// Counts
static let appLaunchCountKey = "APP_OPENED_COUNT"
}
let deault = UserDefaults.standard
struct StoreReviewHelper {
///called from appdelegate didfinishLaunchingWithOptions:
static func incrementAppOpenedCount() {
guard var appOpenCount = deault.value(forKey: UserDefaultsKeys.appLaunchCountKey) as? Int else {
deault.set(1, forKey: UserDefaultsKeys.appLaunchCountKey)
return
}
appOpenCount += 1
deault.set(appOpenCount, forKey: UserDefaultsKeys.appLaunchCountKey)
}
// call this whenever appropriate
static func checkAndAskForReview() {
// this will not be shown everytime. Apple has some internal logic on how to show this.
guard let appOpenCount = deault.value(forKey: UserDefaultsKeys.appLaunchCountKey) as? Int else {
deault.set(1, forKey: UserDefaultsKeys.appLaunchCountKey)
return
}
switch appOpenCount {
case 10,50:
StoreReviewHelper().requestReview()
case _ where appOpenCount%100 == 0 :
StoreReviewHelper().requestReview()
default:
print("App run count is : \(appOpenCount)")
break;
}
}
fileprivate func requestReview() {
if #available(iOS 10.3, *) {
SKStoreReviewController.requestReview()
} else {
// Fallback on earlier versions
// Try any other 3rd party or manual method here.
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment