Skip to content

Instantly share code, notes, and snippets.

@ozgurshn
Created February 11, 2020 16:47
Show Gist options
  • Select an option

  • Save ozgurshn/1d9078e20b294f795a9e3c0631022b4b to your computer and use it in GitHub Desktop.

Select an option

Save ozgurshn/1d9078e20b294f795a9e3c0631022b4b to your computer and use it in GitHub Desktop.
Ask for AppStore Review
//
// AppstoreReview.swift
// Colorizer
//
// Created by ozgur on 2/11/20.
// Copyright © 2020 Den. All rights reserved.
//
import Foundation
import StoreKit
class AppstoreReview
{
static let processCompletedCountKey = "processCompletedCount"
static let lastVersionPromptedForReviewKey = "lastVersionPromptedForReview"
static func askReviewIfNecessary()
{
let askAfterThisTimesOfProcess = 3
// If the count has not yet been stored, this will return 0
var count = UserDefaults.standard.integer(forKey: processCompletedCountKey)
count += 1
UserDefaults.standard.set(count, forKey: processCompletedCountKey)
print("Process completed \(count) time(s)")
// Get the current bundle version for the app
let infoDictionaryKey = kCFBundleVersionKey as String
guard let currentVersion = Bundle.main.object(forInfoDictionaryKey: infoDictionaryKey) as? String
else { fatalError("Expected to find a bundle version in the info dictionary") }
let lastVersionPromptedForReview = UserDefaults.standard.string(forKey: lastVersionPromptedForReviewKey)
// Has the process been completed several times and the user has not already been prompted for this version?
if count >= askAfterThisTimesOfProcess
&& currentVersion != lastVersionPromptedForReview {
let twoSecondsFromNow = DispatchTime.now() + 2.0
DispatchQueue.main.asyncAfter(deadline: twoSecondsFromNow) {
SKStoreReviewController.requestReview()
UserDefaults.standard.set(currentVersion, forKey: self.lastVersionPromptedForReviewKey)
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment