Last active
March 30, 2020 18:42
-
-
Save arbel03/0225de018d98df5f5dfcfd730ad7782e to your computer and use it in GitHub Desktop.
A customizable Swift class that triggers a dialog for asking the user to rate the app.
This file contains 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
// | |
// AppRater.swift | |
// EasyGraphCalculator_iOS | |
// | |
// Created by arbel03 on 23/03/2016. | |
// Copyright © 2016 arbel03. All rights reserved. | |
// | |
import UIKit | |
class AppRater { | |
static let kFirstDateOpened = "firstDateOpenedKey" | |
static let kAmountOpened = "amountOpenedKey" | |
static let kShouldDisplay = "shouldDisplayKey" | |
static let defaults = NSUserDefaults.standardUserDefaults() | |
//Insert App Name | |
static let appName = "" | |
//Insert App ID | |
static let appID = "" | |
//Choose amount of days before showing the rate dialog | |
static let DAYS_BEFORE_SHOWING = 2 | |
//Show the amount of times the app should open before | |
//presenting the dialog | |
static let AMOUNT_OPENED_BEFORE_SHOWING = 3 | |
//MARK: - Put this in the viewDidAppear(_:) method of your main view controller | |
static func appAppeared(sourceViewController: UIViewController){ | |
//Getting the date when the app was first launched and the current date | |
let firstDateOpened = defaults.objectForKey(kFirstDateOpened) as! NSDate | |
let currentDate = NSDate() | |
//Calculating the amount of days passed since the first launch date | |
let daysPassed = NSCalendar.currentCalendar().components(.Day, fromDate: firstDateOpened, toDate: currentDate, options: []).day | |
//Getting the controller variable for the should display button | |
let shouldDisplay = defaults.boolForKey(kShouldDisplay) | |
//Getting the amount the app has been opened | |
let amountOpened = defaults.integerForKey(kAmountOpened) | |
//Launching the dialog if needed | |
if amountOpened >= AMOUNT_OPENED_BEFORE_SHOWING && daysPassed >= DAYS_BEFORE_SHOWING && shouldDisplay { | |
let alertView = UIAlertController(title: "Rate \(appName)", message: "If you enjoy \(appName) would you mind taking a moment to rate it? It won't take more than a minute. Thanks for your support!", preferredStyle: .Alert) | |
alertView.addAction(UIAlertAction(title: "Rate It Now", style: .Default, handler: { | |
(action) -> Void in | |
let url = NSURL(string: "itms-apps://itunes.apple.com/app/id" + appID)! | |
if UIApplication.sharedApplication().canOpenURL(url) { | |
UIApplication.sharedApplication().openURL(url) | |
} | |
})) | |
alertView.addAction(UIAlertAction(title: "No, Thanks", style: .Default, handler: { | |
(action) -> Void in | |
defaults.setBool(false, forKey: kShouldDisplay) | |
})) | |
alertView.addAction(UIAlertAction(title: "Remind Me Later", style: .Cancel, handler: nil)) | |
sourceViewController.presentViewController(alertView, animated: true, completion: nil) | |
resetCounters() | |
} | |
} | |
//MARK: Put this in the viewDidLoad(:) method of your main view controller | |
static func appLaunched(){ | |
//Instantiating if needed | |
instantiateIfNeeded() | |
//Then incrementing each time the app is being opened the amount of opened times | |
let amountOpened = defaults.integerForKey(kAmountOpened) | |
defaults.setInteger(amountOpened+1, forKey: kAmountOpened) | |
} | |
//MARK: - | |
static func instantiateIfNeeded() { | |
if defaults.valueForKey(kFirstDateOpened) == nil{ | |
defaults.setValue(NSDate(), forKey: kFirstDateOpened) | |
} | |
if defaults.valueForKey(kAmountOpened) == nil { | |
defaults.setInteger(0, forKey: kAmountOpened) | |
} | |
if defaults.valueForKey(kShouldDisplay) == nil { | |
defaults.setBool(true, forKey: kShouldDisplay) | |
} | |
} | |
static func resetCounters() { | |
//Resetting the first date opened to the current date | |
defaults.setObject(NSDate(), forKey: kFirstDateOpened) | |
//Resetting the amount of opened time | |
defaults.setInteger(0, forKey: kAmountOpened) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment