Forked from abhimuralidharan/StoreReviewHelper.swift
Created
August 29, 2018 21:54
-
-
Save MehdiChennoufi/985fbcdf235fc24009a243e227e88bd8 to your computer and use it in GitHub Desktop.
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
// | |
// 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. | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment