forked from arashpayan/appirater
-
Notifications
You must be signed in to change notification settings - Fork 0
/
SwiftRate.swift
616 lines (517 loc) · 20.8 KB
/
SwiftRate.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
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
/*
This file is part of SwiftRate.
Copyright (c) 2017, Amir Kamali
All rights reserved.
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.
*/
/*
* SwiftRate.swift
* Get Latest version from
* https://github.com/amirmc3/SwiftRate
*
* Created by Amir Kamali on 01/06/2017.
* http://Kamali.io
* Copyright 2017 Arash Payan. All rights reserved.
*/
import Foundation
import UIKit
import SystemConfiguration
class SwiftRate
{
//MARK: - Constants
let kSRFirstUseDate = "kSRFirstUseDate"
let kSRUseCount = "kSRUseCount"
let kSRShortTermEventCount = "kSRLongEventCount"
let kSRLongTermEventCount = "kSRShortEventCount"
let kSRCurrentVersion = "kSRCurrentVersion"
let kSRRatedCurrentVersion = "kSRRatedCurrentVersion"
let kSRDeclinedToRate = "kSRDeclinedToRate"
let kSRReminderRequestDate = "kSRReminderRequestDate"
let templateReviewURL = "itms-apps://itunes.apple.com/WebObjects/MZStore.woa/wa/viewContentsUserReviews?id=APP_ID&onlyLatestVersion=true&pageNumber=0&sortOrdering=1&type=Purple+Software"
//MARK: variables
private var appId = ""
private var daysUntilPrompt:Double = -1
private var usesUntilPrompt:Int = 3
private var shortTermSignificantEventsUntilPrompt = -1
private var longTermSignificantEventsUntilPrompt = -1
private var timeBeforeReminding:Double = 1
private var debug = false
var delegate:swiftRateProtocol?
var alwaysUseMainBundle = false
var alertTitle:String = "Thank you!"
var alertMessage:String = "If you enjoy using this app, would you please take a moment and rate this app on AppStore?"
var alertCancelTitle:String = "Cancel"
var alertRateTitle:String = "Rate"
var alertRateLaterTitle:String = "Remind me later"
var eventQueue:OperationQueue?
var ratingAlert:UIAlertController?
var isAlertViewShowing:Bool = false
private static var _sharedInstance:SwiftRate?;
static func RegisterSwiftRate(debug:Bool = false)
{
_ = sharedInstance
config_DebugMode(debug: debug)
appLaunched(canPromptForRating: true)
}
static var sharedInstance:SwiftRate
{
get
{
if (_sharedInstance == nil)
{
_sharedInstance = SwiftRate()
_sharedInstance?.eventQueue = OperationQueue()
_sharedInstance?.eventQueue?.maxConcurrentOperationCount = 1
NotificationCenter.default.addObserver(self, selector: #selector(appWillResignActive), name: NSNotification.Name.UIApplicationWillResignActive, object: nil)
NotificationCenter.default.addObserver(self, selector: #selector(appEnteredForeground(canPromptForRating:)), name: NSNotification.Name.UIApplicationWillEnterForeground, object: nil)
//Reset short term events
UserDefaults.standard.set(0, forKey: _sharedInstance!.kSRShortTermEventCount)
UserDefaults.standard.synchronize()
}
return _sharedInstance!;
}
}
//MARK:- SwiftRate Configurations
static func config_AppID(appID:String)
{
SwiftRate.sharedInstance.appId = appID
}
static func config_DaysUntilPrompt(days:Double) {
SwiftRate.sharedInstance.daysUntilPrompt = days
}
static func config_DaysUntilPrompt(value:Double) {
SwiftRate.sharedInstance.daysUntilPrompt = value;
}
static func config_UsesUntilPrompt(value:Int) {
SwiftRate.sharedInstance.usesUntilPrompt = value;
}
static func config_shortTermSignificantEventsUntilPrompt(value:NSInteger) {
SwiftRate.sharedInstance.shortTermSignificantEventsUntilPrompt = value;
}
static func config_longTermSignificantEventsUntilPrompt(value:NSInteger) {
SwiftRate.sharedInstance.longTermSignificantEventsUntilPrompt = value;
}
static func config_TimeBeforeReminding(value:Double) {
SwiftRate.sharedInstance.timeBeforeReminding = value
}
static func config_CustomAlertTitle(title:String)
{
SwiftRate.sharedInstance.alertTitle = title;
}
static func config_CustomAlertMessage(message:String)
{
SwiftRate.sharedInstance.alertMessage = message;
}
static func config_CustomAlertCancelButtonTitle(cancelTitle:String)
{
SwiftRate.sharedInstance.alertCancelTitle = cancelTitle;
}
static func config_CustomAlertRateButtonTitle(rateTitle:String)
{
SwiftRate.sharedInstance.alertRateTitle = rateTitle;
}
static func config_CustomAlertRateLaterButtonTitle(rateLaterTitle:String)
{
SwiftRate.sharedInstance.alertRateLaterTitle = rateLaterTitle;
}
static func config_DebugMode(debug:Bool)
{
SwiftRate.sharedInstance.debug = debug;
}
static func config_AlwaysUseMainBundle(alwaysUseMainBundle:Bool) {
SwiftRate.sharedInstance.alwaysUseMainBundle = alwaysUseMainBundle;
}
//MARK: - Swift Rate Logics
// is this an ok time to show the alert? (regardless of whether the rating conditions have been met)
//
// things checked here:
// * connectivity with network
// * whether user has rated before
// * whether user has declined to rate
// * whether rating alert is currently showing visibly
// things NOT checked here:
// * time since first launch
// * number of uses of app
// * number of significant events
// * time since last reminder
func ratingAlertIsAppropriate()->Bool
{
return (self.isConnectedToNetwork()
&& !self.userHasDeclinedToRate()
&& !self.isAlertViewShowing
&& !self.userHasRatedCurrentVersion());
}
// have the rating conditions been met/earned? (regardless of whether this would be a moment when it's appropriate to show a new rating alert)
//
// things checked here:
// * time since first launch
// * number of uses of app
// * number of significant events
// * time since last reminder
// things NOT checked here:
// * connectivity with network
// * whether user has rated before
// * whether user has declined to rate
// * whether rating alert is currently showing visibly
func ratingConditionsHaveBeenMet()->Bool
{
if (debug)
{
return true;
}
let userDefaults = UserDefaults.standard
let firstUse = userDefaults.double(forKey: kSRFirstUseDate)
let dateOfFirstLaunch = NSDate(timeIntervalSince1970: firstUse)
let timeSinceFirstLaunch = NSDate().timeIntervalSince(dateOfFirstLaunch as Date);
let timeUntilRate = 60 * 60 * 24 * daysUntilPrompt
if (timeSinceFirstLaunch < timeUntilRate)
{
return false
}
// check if the app has been used enough
let useCount = userDefaults.integer(forKey: kSRUseCount);
if (useCount < usesUntilPrompt)
{
return false;
}
// check if the user has done enough significant events in Total
let sigEventCount = userDefaults.integer(forKey:kSRLongTermEventCount)
if (sigEventCount < longTermSignificantEventsUntilPrompt)
{
return false;
}
// check if the user has done enough significant events in recently
let sigShortEventCount = userDefaults.integer(forKey:kSRShortTermEventCount)
if (sigShortEventCount < shortTermSignificantEventsUntilPrompt)
{
return false;
}
// if the user wanted to be reminded later, has enough time passed?
let reminderRequestDate = NSDate(timeIntervalSince1970: userDefaults.double(forKey: kSRReminderRequestDate))
let timeSinceReminderRequest = NSDate().timeIntervalSince(reminderRequestDate as Date)
let timeUntilReminder = 60 * 60 * 24 * timeBeforeReminding;
if (timeSinceReminderRequest < timeUntilReminder)
{
return false
}
return true
}
func incrementUseCount()
{
// get the app's version
let version = getVersion()
// get the version number that we've been tracking
let userDefaults = UserDefaults.standard
var trackingVersion = userDefaults.string(forKey: kSRCurrentVersion)
if (trackingVersion == nil)
{
trackingVersion = version;
userDefaults.set(version, forKey: kSRCurrentVersion)
userDefaults.synchronize()
}
if (debug)
{
print("APPIRATER Tracking version: \(trackingVersion ?? "-")");
}
if (trackingVersion == version)
{
// check if the first use date has been set. if not, set it.
var timeInterval = userDefaults.double(forKey:kSRFirstUseDate);
if (timeInterval == 0)
{
timeInterval = NSDate().timeIntervalSince1970
userDefaults.set(timeInterval, forKey: kSRFirstUseDate)
}
// increment the use count
var useCount = userDefaults.integer(forKey: kSRUseCount)
useCount += 1;
userDefaults.set(useCount, forKey: kSRUseCount)
userDefaults.synchronize()
if (debug)
{
print("APPIRATER Use count: \(useCount)");
}
}
else
{
// it's a new version of the app, so restart tracking
InitializeSettings(version: version)
userDefaults.set(1, forKey: kSRUseCount)
userDefaults.synchronize()
}
}
func InitializeSettings(version:String)
{
// it's a new version of the app, so restart tracking
let userDefaults = UserDefaults.standard
userDefaults.set(version, forKey: kSRCurrentVersion)
userDefaults.set(NSDate().timeIntervalSince1970, forKey: kSRFirstUseDate)
userDefaults.set(0, forKey: kSRUseCount)
userDefaults.set(0, forKey: kSRLongTermEventCount)
userDefaults.set(0, forKey: kSRShortTermEventCount)
userDefaults.set(false, forKey: kSRRatedCurrentVersion)
userDefaults.set(false, forKey: kSRDeclinedToRate)
userDefaults.set(0, forKey: kSRReminderRequestDate)
userDefaults.synchronize()
}
func incrementSignificantEventCount(shortTerm:Bool)
{
// get the app's version
let version = getVersion()
// get the version number that we've been tracking
let userDefaults = UserDefaults.standard
var trackingVersion = userDefaults.string(forKey: kSRCurrentVersion)
if (trackingVersion == nil)
{
trackingVersion = version;
userDefaults.set(version, forKey: kSRCurrentVersion)
}
if (debug)
{
print("APPIRATER Tracking version: \(trackingVersion ?? "-")");
}
if (trackingVersion == version)
{
// check if the first use date has been set. if not, set it.
var timeInterval = userDefaults.double(forKey:kSRFirstUseDate);
if (timeInterval == 0)
{
timeInterval = NSDate().timeIntervalSince1970
userDefaults.set(timeInterval, forKey: kSRFirstUseDate)
}
// increment the significant event count
var sigShortEventCount = userDefaults.integer(forKey:kSRShortTermEventCount);
sigShortEventCount += 1;
userDefaults.set(sigShortEventCount, forKey:kSRShortTermEventCount);
userDefaults.synchronize()
if (debug)
{
print("APPIRATER short term event count: \(sigShortEventCount)");
}
}
else
{
InitializeSettings(version: version)
var shorTermCount = 0
var longTermCount = 0
if (shortTerm)
{
shorTermCount = 1
}
else
{
longTermCount = 1
}
// it's a new version of the app, so restart tracking
userDefaults.set(shorTermCount, forKey: kSRShortTermEventCount)
userDefaults.set(longTermCount, forKey: kSRLongTermEventCount)
userDefaults.synchronize()
}
}
func checkForRate(canPromptForRating:Bool)
{
if (canPromptForRating &&
self.ratingConditionsHaveBeenMet() &&
self.ratingAlertIsAppropriate())
{
DispatchQueue.main.async {
self.showRatingAlert(displayRateLaterButton: true)
}
}
}
func userHasDeclinedToRate()->Bool {
return UserDefaults.standard.bool(forKey: kSRDeclinedToRate)
}
func userHasRatedCurrentVersion() -> Bool {
return UserDefaults.standard.bool(forKey: kSRRatedCurrentVersion)
}
static func userDidSignificantEvent(eventType:swiftRateSignificantEventType)
{
let instance = SwiftRate.sharedInstance
instance.eventQueue?.addOperation {
instance.incrementSignificantEventCount(shortTerm: (eventType == .shortTerm));
instance.checkForRate(canPromptForRating: true)
}
}
static func rateApp()
{
let userDefaults = UserDefaults.standard
userDefaults.set(true, forKey: SwiftRate.sharedInstance.kSRRatedCurrentVersion)
userDefaults.synchronize()
let reviewURL:String = SwiftRate.sharedInstance.templateReviewURL.replacingOccurrences(of: "APP_ID", with: "\(sharedInstance.appId)")
UIApplication.shared.open(URL(string: reviewURL)!, options: [:], completionHandler: nil)
}
//MARK: - AppDelegate Events
static func appLaunched(canPromptForRating:Bool)
{
DispatchQueue.main.async {
let instance = SwiftRate.sharedInstance;
if (instance.debug) {
instance.showRatingAlert(displayRateLaterButton: true)
}
else {
instance.incrementUseCount()
instance.checkForRate(canPromptForRating: canPromptForRating)
}
}
}
@objc static func appWillResignActive() {
if (SwiftRate.sharedInstance.debug)
{
print("APPIRATER appWillResignActive")
}
SwiftRate.sharedInstance.hideRatingAlert()
}
@objc static func appEnteredForeground(canPromptForRating:Bool) {
let instance = SwiftRate.sharedInstance
instance.eventQueue?.addOperation {
instance.incrementUseCount()
instance.checkForRate(canPromptForRating: canPromptForRating)
}
}
//MARK: - Network
func isConnectedToNetwork() -> Bool {
var zeroAddress = sockaddr_in(sin_len: 0, sin_family: 0, sin_port: 0, sin_addr: in_addr(s_addr: 0), sin_zero: (0, 0, 0, 0, 0, 0, 0, 0))
zeroAddress.sin_len = UInt8(MemoryLayout.size(ofValue: zeroAddress))
zeroAddress.sin_family = sa_family_t(AF_INET)
let defaultRouteReachability = withUnsafePointer(to: &zeroAddress) {
$0.withMemoryRebound(to: sockaddr.self, capacity: 1) {zeroSockAddress in
SCNetworkReachabilityCreateWithAddress(nil, zeroSockAddress)
}
}
var flags: SCNetworkReachabilityFlags = SCNetworkReachabilityFlags(rawValue: 0)
if SCNetworkReachabilityGetFlags(defaultRouteReachability!, &flags) == false {
return false
}
/* Only Working for WIFI
let isReachable = flags == .reachable
let needsConnection = flags == .connectionRequired
return isReachable && !needsConnection
*/
// Working for Cellular and WIFI
let isReachable = (flags.rawValue & UInt32(kSCNetworkFlagsReachable)) != 0
let needsConnection = (flags.rawValue & UInt32(kSCNetworkFlagsConnectionRequired)) != 0
let ret = (isReachable && !needsConnection)
return ret
}
//MARK: - UI
func showRatingAlert(displayRateLaterButton:Bool)
{
let alertView = UIAlertController(title: self.alertTitle, message: self.alertMessage, preferredStyle: .alert)
let action_Rate = UIAlertAction(title: self.alertRateTitle, style: .default, handler: { (action) in
//User accepted rating
SwiftRate.rateApp()
if(self.delegate != nil){
self.delegate?.didOptForRating()
}
self.isAlertViewShowing = false
})
alertView.addAction(action_Rate)
if (displayRateLaterButton) {
//Maybe later
let action = UIAlertAction(title: self.alertRateLaterTitle, style: .default, handler: { (action) in
UserDefaults.standard.set(Date().timeIntervalSince1970, forKey: self.kSRReminderRequestDate)
UserDefaults.standard.synchronize()
if (self.delegate != nil)
{
self.delegate?.DidOptToRemindLater()
}
self.isAlertViewShowing = false
})
alertView.addAction(action)
}
let action_Cancel = UIAlertAction(title: self.alertCancelTitle, style: .cancel, handler: { (action) in
//Cancel Rate
UserDefaults.standard.set(true, forKey: self.kSRDeclinedToRate)
UserDefaults.standard.synchronize()
if (self.delegate != nil)
{
self.delegate?.DidDeclineToRate()
}
self.isAlertViewShowing = false
})
alertView.addAction(action_Cancel)
self.ratingAlert = alertView
isAlertViewShowing = true
topMostViewController()?.present(ratingAlert!, animated: true, completion: nil)
if (delegate != nil)
{
delegate?.SwiftRateDidDisplayAlert(alert: ratingAlert!)
}
}
func hideRatingAlert() {
if (isAlertViewShowing) {
if (debug)
{
print("APPIRATER Hiding Alert")
}
ratingAlert?.dismiss(animated: false, completion: nil)
}
}
func topMostViewController() ->UIViewController?
{
if var topController = UIApplication.shared.keyWindow?.rootViewController {
while let presentedViewController = topController.presentedViewController {
topController = presentedViewController
}
return topController
}
return nil
}
//MARK: - App Info
func getVersion()->String
{
return Bundle.main.object(forInfoDictionaryKey: String( kCFBundleVersionKey)) as! String
}
static var bundle:Bundle
{
get
{
var bundle:Bundle?
if (SwiftRate.sharedInstance.alwaysUseMainBundle) {
bundle = Bundle.main;
}
else
{
if let bundleURL = Bundle.main.url(forResource: "SwiftRate", withExtension: "bundle")
{
// SwiftRate.bundle will likely only exist when used via CocoaPods
bundle = Bundle(url: bundleURL)
}
else {
bundle = Bundle.main;
}
}
return bundle!
}
}
}
//MARK:- Protocols
protocol swiftRateProtocol {
func SwiftRateDidDisplayAlert(alert:UIAlertController)
func didOptForRating()
func DidOptToRemindLater()
func DidDeclineToRate()
}
enum swiftRateSignificantEventType
{
case shortTerm
case longTerm
}