Skip to content

Instantly share code, notes, and snippets.

@abhimuralidharan
Last active May 15, 2020 19:06
Show Gist options
  • Save abhimuralidharan/fc717fb27d1d7388524e70a09860a786 to your computer and use it in GitHub Desktop.
Save abhimuralidharan/fc717fb27d1d7388524e70a09860a786 to your computer and use it in GitHub Desktop.
//
// StoreReviewHelper.swift
// Template1
//
// Created by Apple on 14/11/17.
// Copyright © 2017 Mobiotics. All rights reserved.
//
import Foundation
import StoreKit
struct StoreReviewHelper {
static func incrementAppOpenedCount() { // called from appdelegate didfinishLaunchingWithOptions:
guard var appOpenCount = Defaults.value(forKey: UserDefaultsKeys.APP_OPENED_COUNT) as? Int else {
Defaults.set(1, forKey: UserDefaultsKeys.APP_OPENED_COUNT)
return
}
appOpenCount += 1
Defaults.set(appOpenCount, forKey: UserDefaultsKeys.APP_OPENED_COUNT)
}
static func checkAndAskForReview() { // call this whenever appropriate
// this will not be shown everytime. Apple has some internal logic on how to show this.
guard var appOpenCount = Defaults.value(forKey: UserDefaultsKeys.APP_OPENED_COUNT) as? Int else {
Defaults.set(1, forKey: UserDefaultsKeys.APP_OPENED_COUNT)
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.
}
}
}
@matrixreal
Copy link

how to implement this to the mainviewcontroller ?

@kjoe07
Copy link

kjoe07 commented Apr 17, 2018

Use of unresolved identifier 'UserDefaultsKeys'
Use of unresolved identifier 'Defaults'
any idea why xcode 9.2 does not reconigze this

@hackzilla
Copy link

I found from another page, that it's just a struct.

import Foundation

struct UserDefaultsKeys {
    // Reviews
    static let lastVersionPromptedForReviewKey = "LAST_VERSION_PROMOTED_FOR_REVIEW"
    
    // Counts
    static let appLaunchCountKey = "APP_OPENED_COUNT"
}

@thomasjad
Copy link

Is there a license for this file? Thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment