-
Notifications
You must be signed in to change notification settings - Fork 27
/
Copy pathRateMyApp.swift
executable file
·331 lines (231 loc) · 10.1 KB
/
RateMyApp.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
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
//
// RateMyApp.swift
// RateMyApp
//
// Created by Jimmy Jose on 08/09/14.
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.
import UIKit
class RateMyApp : UIViewController,UIAlertViewDelegate{
fileprivate let kTrackingAppVersion = "kRateMyApp_TrackingAppVersion"
fileprivate let kFirstUseDate = "kRateMyApp_FirstUseDate"
fileprivate let kAppUseCount = "kRateMyApp_AppUseCount"
fileprivate let kSpecialEventCount = "kRateMyApp_SpecialEventCount"
fileprivate let kDidRateVersion = "kRateMyApp_DidRateVersion"
fileprivate let kDeclinedToRate = "kRateMyApp_DeclinedToRate"
fileprivate let kRemindLater = "kRateMyApp_RemindLater"
fileprivate let kRemindLaterPressedDate = "kRateMyApp_RemindLaterPressedDate"
fileprivate var reviewURL = "itms-apps://ax.itunes.apple.com/WebObjects/MZStore.woa/wa/viewContentsUserReviews?type=Purple+Software&id="
fileprivate var reviewURLiOS7 = "itms-apps://itunes.apple.com/app/id"
var promptAfterDays:Double = 30
var promptAfterUses = 10
var promptAfterCustomEventsCount = 10
var daysBeforeReminding:Double = 1
var alertTitle = NSLocalizedString("Rate the app", comment: "RateMyApp")
var alertMessage = ""
var alertOKTitle = NSLocalizedString("Rate it now", comment: "RateMyApp")
var alertCancelTitle = NSLocalizedString("Don't bother me again", comment: "RateMyApp")
var alertRemindLaterTitle = NSLocalizedString("Remind me later", comment: "RateMyApp")
var appID = ""
var debug = false
class var sharedInstance : RateMyApp {
struct Static {
static let instance : RateMyApp = RateMyApp()
}
return Static.instance
}
// private override init(){
//
// super.init()
//
// }
internal required init?(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)
}
fileprivate override init(nibName nibNameOrNil: String?, bundle nibBundleOrNil: Bundle?) {
super.init(nibName: nibNameOrNil, bundle: nibBundleOrNil)
}
fileprivate func initAllSettings(){
let prefs = UserDefaults.standard
prefs.set(getCurrentAppVersion(), forKey: kTrackingAppVersion)
prefs.set(Date(), forKey: kFirstUseDate)
prefs.set(1, forKey: kAppUseCount)
prefs.set(0, forKey: kSpecialEventCount)
prefs.set(false, forKey: kDidRateVersion)
prefs.set(false, forKey: kDeclinedToRate)
prefs.set(false, forKey: kRemindLater)
}
func trackEventUsage(){
incrementValueForKey(name: kSpecialEventCount)
}
func trackAppUsage(){
incrementValueForKey(name: kAppUseCount)
}
fileprivate func isFirstTime()->Bool{
let prefs = UserDefaults.standard
let trackingAppVersion = prefs.object(forKey: kTrackingAppVersion) as? NSString
if((trackingAppVersion == nil) || !(getCurrentAppVersion().isEqual(to: trackingAppVersion! as String)))
{
return true
}
return false
}
fileprivate func incrementValueForKey(name:String){
if(appID.count == 0)
{
fatalError("Set iTunes connect appID to proceed, you may enter some random string for testing purpose. See line number 59")
}
if(isFirstTime())
{
initAllSettings()
}
else
{
let prefs = UserDefaults.standard
let currentCount = prefs.integer(forKey: name)
prefs.set(currentCount+1, forKey: name)
}
if(shouldShowAlert())
{
showRatingAlert()
}
}
fileprivate func shouldShowAlert() -> Bool{
if debug {
return true
}
let prefs = UserDefaults.standard
let usageCount = prefs.integer(forKey: kAppUseCount)
let eventsCount = prefs.integer(forKey: kSpecialEventCount)
let firstUse = prefs.object(forKey: kFirstUseDate) as! Date
let timeInterval = Date().timeIntervalSince(firstUse)
let daysCount = ((timeInterval / 3600) / 24)
let hasRatedCurrentVersion = prefs.bool(forKey: kDidRateVersion)
let hasDeclinedToRate = prefs.bool(forKey: kDeclinedToRate)
let hasChosenRemindLater = prefs.bool(forKey: kRemindLater)
if(hasDeclinedToRate)
{
return false
}
if(hasRatedCurrentVersion)
{
return false
}
if(hasChosenRemindLater)
{
let remindLaterDate = prefs.object(forKey: kRemindLaterPressedDate) as! Date
let timeInterval = Date().timeIntervalSince(remindLaterDate)
let remindLaterDaysCount = ((timeInterval / 3600) / 24)
return (remindLaterDaysCount >= daysBeforeReminding)
}
if(usageCount >= promptAfterUses)
{
return true
}
if(daysCount >= promptAfterDays)
{
return true
}
if(eventsCount >= promptAfterCustomEventsCount)
{
return true
}
return false
}
fileprivate func showRatingAlert(){
let infoDocs : NSDictionary = Bundle.main.infoDictionary! as NSDictionary
let appname : NSString = infoDocs.object(forKey: "CFBundleName") as! NSString
var message = NSLocalizedString("If you found %@ useful, please take a moment to rate it", comment: "RateMyApp")
message = String(format:message, appname)
if(alertMessage.count == 0)
{
alertMessage = message
}
if #available(iOS 8.0, *) {
let alert = UIAlertController(title: alertTitle, message: alertMessage, preferredStyle: UIAlertControllerStyle.alert)
alert.addAction(UIAlertAction(title: alertOKTitle, style:.destructive, handler: { alertAction in
self.okButtonPressed()
alert.dismiss(animated: true, completion: nil)
}))
alert.addAction(UIAlertAction(title: alertCancelTitle, style:.cancel, handler:{ alertAction in
self.cancelButtonPressed()
alert.dismiss(animated: true, completion: nil)
}))
alert.addAction(UIAlertAction(title: alertRemindLaterTitle, style:.default, handler: { alertAction in
self.remindLaterButtonPressed()
alert.dismiss(animated: true, completion: nil)
}))
let appDelegate = UIApplication.shared.delegate as! AppDelegate
let controller = appDelegate.window?.rootViewController
controller?.present(alert, animated: true, completion: nil)
} else {
let alert = UIAlertView()
alert.title = alertTitle
alert.message = alertMessage
alert.addButton(withTitle: alertCancelTitle)
alert.addButton(withTitle: alertRemindLaterTitle)
alert.addButton(withTitle: alertOKTitle)
alert.delegate = self
alert.show()
}
}
internal func alertView(_ alertView: UIAlertView, clickedButtonAt buttonIndex: Int){
if(buttonIndex == 0)
{
cancelButtonPressed()
}
else if(buttonIndex == 1)
{
remindLaterButtonPressed()
}
else if(buttonIndex == 2)
{
okButtonPressed()
}
alertView.dismiss(withClickedButtonIndex: buttonIndex, animated: true)
}
fileprivate func deviceOSVersion() -> Float{
let device : UIDevice = UIDevice.current;
let systemVersion = device.systemVersion;
let iOSVerion : Float = (systemVersion as NSString).floatValue
return iOSVerion
}
fileprivate func hasOS8()->Bool{
if(deviceOSVersion() < 8.0)
{
return false
}
return true
}
fileprivate func okButtonPressed(){
UserDefaults.standard.set(true, forKey: kDidRateVersion)
let appStoreURL = URL(string:reviewURLiOS7+appID)
UIApplication.shared.openURL(appStoreURL!)
}
fileprivate func cancelButtonPressed(){
UserDefaults.standard.set(true, forKey: kDeclinedToRate)
}
fileprivate func remindLaterButtonPressed(){
UserDefaults.standard.set(true, forKey: kRemindLater)
UserDefaults.standard.set(Date(), forKey: kRemindLaterPressedDate)
}
fileprivate func getCurrentAppVersion()->NSString{
return (Bundle.main.object(forInfoDictionaryKey: "CFBundleShortVersionString") as! NSString)
}
}