@@ -15,47 +15,46 @@ public enum VersionCheckType: Int {
15
15
case weekly = 7
16
16
}
17
17
18
- struct AppStoreDefaults {
18
+ enum AppStoreDefaults {
19
19
static let storedVersionCheckDate = " storedVersionCheckDate "
20
20
static let storedSkippedVersion = " storedSkippedVersion "
21
21
}
22
22
23
23
public class AppStoreManager {
24
-
25
24
public static let shared = AppStoreManager ( )
26
-
25
+
27
26
var title : String = AppStoreManagerConstant . alertTitle
28
27
var message : String ? = AppStoreManagerConstant . alertMessage
29
-
28
+
30
29
var skipButtonTitle : String = AppStoreManagerConstant . skipButtonTitle
31
30
var updateButtonTitle : String = AppStoreManagerConstant . updateButtonTitle
32
-
31
+
33
32
var lastVersionCheckDate : Date ? {
34
- didSet{
33
+ didSet {
35
34
UserDefaults . standard. set ( self . lastVersionCheckDate, forKey: AppStoreDefaults . storedVersionCheckDate)
36
35
UserDefaults . standard. synchronize ( )
37
36
}
38
37
}
39
-
38
+
40
39
let bundleId = Bundle . main. bundleIdentifier ?? " "
41
-
40
+
42
41
var currentInstalledVersion : String ? {
43
42
return Bundle . main. object ( forInfoDictionaryKey: " CFBundleShortVersionString " ) as? String
44
43
}
45
-
44
+
46
45
var appStoreResult : AppStoreResult ?
47
-
46
+
48
47
init ( ) {
49
48
self . lastVersionCheckDate = UserDefaults . standard. object ( forKey: AppStoreDefaults . storedVersionCheckDate) as? Date
50
49
}
51
-
52
- func getStoreVersion( completion: @escaping ( AppStoreResult ? ) -> ( ) ) {
50
+
51
+ func getStoreVersion( completion: @escaping ( AppStoreResult ? ) -> Void ) {
53
52
guard let url = URL ( string: " https://itunes.apple.com/lookup?bundleId= \( self . bundleId) " ) else {
54
53
completion ( nil )
55
54
return
56
55
}
57
56
let session = URLSession ( configuration: . default)
58
- let task = session. dataTask ( with: url) { [ weak self] ( data, response , error) in
57
+ let task = session. dataTask ( with: url) { [ weak self] data, _ , error in
59
58
if let er = error {
60
59
self ? . log ( er. localizedDescription)
61
60
completion ( nil )
@@ -67,16 +66,16 @@ public class AppStoreManager {
67
66
self ? . log ( " AppStore version: \( result. version ?? " " ) " )
68
67
self ? . appStoreResult = result
69
68
completion ( result)
70
- } else {
69
+ } else {
71
70
self ? . appStoreResult = nil
72
71
completion ( nil )
73
72
}
74
73
}
75
74
task. resume ( )
76
75
}
77
-
78
- public func checkNewVersion( _ type: VersionCheckType , isAvailable: @escaping ( Bool ) -> ( ) ) {
79
- self . getStoreVersion { [ weak self] ( result) in
76
+
77
+ public func checkNewVersion( _ type: VersionCheckType , isAvailable: @escaping ( Bool ) -> Void ) {
78
+ self . getStoreVersion { [ weak self] result in
80
79
if let currentInstalledVersion = self ? . currentInstalledVersion,
81
80
let appStoreVersion = result? . version {
82
81
switch currentInstalledVersion. compare ( appStoreVersion, options: . numeric) {
@@ -94,24 +93,24 @@ public class AppStoreManager {
94
93
if Date . days ( since: lastVersionCheckDate) >= type. rawValue {
95
94
self ? . lastVersionCheckDate = Date ( )
96
95
isAvailable ( true )
97
- } else {
96
+ } else {
98
97
isAvailable ( false )
99
98
}
100
99
}
101
100
case . orderedDescending, . orderedSame:
102
101
isAvailable ( false )
103
102
}
104
- } else {
103
+ } else {
105
104
isAvailable ( false )
106
105
}
107
106
}
108
107
}
109
-
108
+
110
109
public func checkNewVersionAndShowAlert( _ type: VersionCheckType ,
111
110
at vc: UIViewController ,
112
111
canSkip: Bool ,
113
112
preferredStyle: UIAlertController . Style = . alert) {
114
- self . getStoreVersion { [ weak self] ( result) in
113
+ self . getStoreVersion { [ weak self] result in
115
114
if let currentInstalledVersion = self ? . currentInstalledVersion,
116
115
let appStoreVersion = result? . version {
117
116
switch currentInstalledVersion. compare ( appStoreVersion, options: . numeric) {
@@ -121,31 +120,31 @@ public class AppStoreManager {
121
120
case . orderedDescending, . orderedSame:
122
121
break
123
122
}
124
- } else {
123
+ } else {
125
124
self ? . log ( " Can't get Version " )
126
125
}
127
126
}
128
127
}
129
-
130
- //MARK: - Alert
131
-
128
+
129
+ // MARK: - Alert
130
+
132
131
public func configureAlert( title: String ? , message: String ? ) {
133
132
self . title = title ?? AppStoreManagerConstant . alertTitle
134
133
self . message = message
135
134
}
136
-
135
+
137
136
public func configureAlert( updateButtonTitle: String ? , skipButtonTitle: String ? ) {
138
137
self . updateButtonTitle = updateButtonTitle ?? AppStoreManagerConstant . updateButtonTitle
139
138
self . skipButtonTitle = skipButtonTitle ?? AppStoreManagerConstant . skipButtonTitle
140
139
}
141
-
142
- public func showAlertUpdate( at vc: UIViewController , canSkip: Bool , preferredStyle: UIAlertController . Style = . alert) {
140
+
141
+ public func showAlertUpdate( at vc: UIViewController , canSkip: Bool , preferredStyle: UIAlertController . Style = . alert) {
143
142
DispatchQueue . main. async { [ weak self] in
144
143
let alertVc = UIAlertController ( title: self ? . title, message: self ? . message, preferredStyle: preferredStyle)
145
- let skip = UIAlertAction ( title: self ? . skipButtonTitle ?? AppStoreManagerConstant . skipButtonTitle, style: . cancel) { ( _ ) in
144
+ let skip = UIAlertAction ( title: self ? . skipButtonTitle ?? AppStoreManagerConstant . skipButtonTitle, style: . cancel) { _ in
146
145
//
147
146
}
148
- let update = UIAlertAction ( title: self ? . updateButtonTitle ?? AppStoreManagerConstant . updateButtonTitle, style: . default) { ( _ ) in
147
+ let update = UIAlertAction ( title: self ? . updateButtonTitle ?? AppStoreManagerConstant . updateButtonTitle, style: . default) { _ in
149
148
self ? . openAppStore ( )
150
149
}
151
150
alertVc. addAction ( update)
@@ -155,12 +154,12 @@ public class AppStoreManager {
155
154
vc. present ( alertVc, animated: true , completion: nil )
156
155
}
157
156
}
158
-
157
+
159
158
public func openAppStore( ) {
160
159
if let appStoreId = self . appStoreResult? . trackId {
161
160
self . openAppStore ( id: appStoreId)
162
- } else {
163
- self . getStoreVersion { [ weak self] ( result) in
161
+ } else {
162
+ self . getStoreVersion { [ weak self] result in
164
163
guard let appStoreId = result? . trackId else {
165
164
self ? . log ( " Can't get an AppId " )
166
165
return
@@ -169,21 +168,19 @@ public class AppStoreManager {
169
168
}
170
169
}
171
170
}
172
-
171
+
173
172
func openAppStore( id appStoreId: Int ) {
174
173
if let url = URL ( string: " https://itunes.apple.com/app/id \( appStoreId) " ) ,
175
174
UIApplication . shared. canOpenURL ( url) {
176
175
UIApplication . shared. open ( url, options: [ : ] , completionHandler: nil )
177
- } else {
176
+ } else {
178
177
self . log ( " Can't open AppStore " )
179
178
}
180
179
}
181
-
180
+
182
181
func log( _ value: String ) {
183
182
print ( " 📲: [AppStore] => \( value) " )
184
183
}
185
-
186
184
}
187
185
188
-
189
186
#endif
0 commit comments