-
Notifications
You must be signed in to change notification settings - Fork 0
/
Rater.swift
122 lines (99 loc) · 3.53 KB
/
Rater.swift
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
//
// Rater.swift
// CrabApp
//
// Created by Oleg Kubrakov on 2/12/2016.
// Copyright © 2016 Oleg Kubrakov. All rights reserved.
//
import UIKit
import StoreKit
let kAppLaunchNumber = "rater_app_launch_number"
let kAppFirstLaunchDate = "rater_app_first_launch_date"
let kAppRatingShown = "rater_app_rating_shown"
let kRequiredLaunchesBeforeRating = "rater-required-launches-before-rating"
public class Rater: NSObject {
var application: UIApplication!
var userdefaults = UserDefaults()
var requiredLaunchesBeforeRating = 3
public var appId: String!
public static var sharedInstance = Rater()
//MARK: - Initialize
public override init() {
super.init()
requiredLaunchesBeforeRating = Config.read(value: kRequiredLaunchesBeforeRating) as! Int
setup()
}
private func setup() {
if (!hasShownAppRating()) {
NotificationCenter.default.addObserver(self, selector: #selector(Rater.appDidFinishLaunching(notification:)), name: NSNotification.Name.UIApplicationDidFinishLaunching, object: nil)
}
}
func appDidFinishLaunching(notification: NSNotification) {
if let _application = notification.object as? UIApplication {
application = _application
displayRatingPromptIfRequired()
}
}
private func displayRatingPromptIfRequired() {
let launches = getAppLaunchCount()
print("launches=\(launches), required=\(requiredLaunchesBeforeRating)")
if launches >= requiredLaunchesBeforeRating && !hasShownAppRating() {
if #available(iOS 10.3, *) {
SKStoreReviewController.requestReview()
} else {
rateTheApp()
}
}
incrementAppLaunches()
}
private func rateTheApp() {
let message = "Enjoying Calm Money? Please rate us. It helps us to provide better updates.".localized
let rateAlert = UIAlertController(title:"Rate us".localized, message: message, preferredStyle: .alert)
rateAlert.addAction(UIAlertAction(title: "Rate".localized, style: .default, handler: { (action) -> Void in
self.openRatePage()
self.setAppRatingShown()
}))
rateAlert.addAction(UIAlertAction(title: "Not Now".localized, style: .cancel, handler: { (action) -> Void in
self.resetAppLaunches()
}))
DispatchQueue.main.asyncAfter(deadline: DispatchTime.now() + .seconds(20), execute: {
let window = self.application.windows[0]
window.rootViewController?.present(rateAlert, animated: true, completion: nil)
})
}
func openRatePage() {
// https://itunes.apple.com/app/calm-money-finance-tracker/id1182713741
let url = NSURL(string: "itms-apps://itunes.apple.com/app/calm-money-finance-tracker/id\(Config.read(value: "appstore-id")!)")
UIApplication.shared.openURL(url! as URL)
}
//MARK: - App Launch Count
private func getAppLaunchCount() -> Int{
return userdefaults.integer(forKey: kAppLaunchNumber)
}
private func incrementAppLaunches() {
var launches = userdefaults.integer(forKey: kAppLaunchNumber)
launches += 1
userdefaults.set(launches, forKey: kAppLaunchNumber)
}
private func resetAppLaunches() {
userdefaults.set(0, forKey: kAppLaunchNumber)
}
//MARK: - Firset Launch Date
private func setFirstLaunchDate() {
userdefaults.setValue(NSDate(), forKey: kAppFirstLaunchDate)
}
private func getFirstLaunchDate() -> NSDate {
if let date = userdefaults.value(forKey: kAppFirstLaunchDate) as? NSDate {
return date
}
return NSDate()
}
//MARK: - App Rating Shown
private func setAppRatingShown() {
userdefaults.set(true, forKey: kAppRatingShown)
userdefaults.synchronize()
}
private func hasShownAppRating() -> Bool {
return userdefaults.bool(forKey: kAppRatingShown)
}
}