@@ -10,7 +10,7 @@ import Foundation
10
10
import AdSupport
11
11
import StoreKit
12
12
13
- let sdk_version = " 0.6.0 "
13
+ let sdk_version = " 0.6.1 "
14
14
15
15
final class ApphudInternal {
16
16
@@ -72,34 +72,14 @@ final class ApphudInternal {
72
72
}
73
73
74
74
private func continueToRegisteringUser( ) {
75
- registerUser { ( result, dictionary, error) in
76
-
77
- var hasSubscriptionChanges = false
78
- if result {
79
- hasSubscriptionChanges = self . parseUser ( dictionary)
80
- }
81
-
82
- if result && self . currentUser != nil {
83
-
84
- if hasSubscriptionChanges {
85
- self . delegate? . apphudSubscriptionsUpdated ? ( self . currentUser!. subscriptions!)
86
- }
87
-
75
+ createOrGetUser { success in
76
+ if success {
88
77
apphudLog ( " User successfully registered " )
89
-
90
- self . performAllUserRegisteredBlocks ( )
91
-
92
- if UserDefaults . standard. bool ( forKey: self . requiresReceiptSubmissionKey) {
93
- self . submitAppStoreReceipt ( allowsReceiptRefresh: false )
94
- }
95
-
78
+ self . performAllUserRegisteredBlocks ( )
96
79
self . continueToUpdateProducts ( )
97
-
98
80
self . listenForAwakeNotification ( )
99
81
self . checkForUnreadNotifications ( )
100
-
101
- } else {
102
- apphudLog ( " Failed to register user, error: \( error? . localizedDescription ?? " " ) " )
82
+ } else {
103
83
self . userRegisteredCallbacks. removeAll ( )
104
84
}
105
85
}
@@ -152,6 +132,7 @@ final class ApphudInternal {
152
132
153
133
if Date ( ) . timeIntervalSince ( lastCheckDate) > minCheckInterval{
154
134
self . checkForUnreadNotifications ( )
135
+ self . refreshCurrentUser ( )
155
136
}
156
137
}
157
138
@@ -245,7 +226,7 @@ final class ApphudInternal {
245
226
246
227
// MARK: API Requests
247
228
248
- private func registerUser ( callback: @escaping ApphudBoolDictionaryCallback ) {
229
+ private func createOrGetUser ( callback: @escaping ( Bool ) -> Void ) {
249
230
250
231
var params : [ String : String ] = [ " device_id " : self . currentDeviceID]
251
232
if self . currentUserID != nil {
@@ -255,7 +236,30 @@ final class ApphudInternal {
255
236
let deviceParams = currentDeviceParameters ( )
256
237
params. merge ( deviceParams) { ( current, new) in current}
257
238
258
- httpClient. startRequest ( path: " customers " , params: params, method: . post, callback: callback)
239
+ httpClient. startRequest ( path: " customers " , params: params, method: . post) { ( result, response, error) in
240
+
241
+ var hasSubscriptionChanges = false
242
+ if result {
243
+ hasSubscriptionChanges = self . parseUser ( response)
244
+ }
245
+
246
+ let finalResult = result && self . currentUser != nil
247
+
248
+ if finalResult {
249
+ if hasSubscriptionChanges {
250
+ self . delegate? . apphudSubscriptionsUpdated ? ( self . currentUser!. subscriptions!)
251
+ }
252
+ if UserDefaults . standard. bool ( forKey: self . requiresReceiptSubmissionKey) {
253
+ self . submitAppStoreReceipt ( allowsReceiptRefresh: false )
254
+ }
255
+ }
256
+
257
+ if error != nil {
258
+ apphudLog ( " Failed to register or get user, error: \( error!. localizedDescription) " )
259
+ }
260
+
261
+ callback ( finalResult)
262
+ }
259
263
}
260
264
261
265
private func updateUserCurrencyIfNeeded( priceLocale : Locale ? ) {
@@ -301,8 +305,8 @@ final class ApphudInternal {
301
305
httpClient. startRequest ( path: " customers " , params: params, method: . post, callback: callback)
302
306
}
303
307
304
- private func getCurrentUser ( ) {
305
-
308
+ private func refreshCurrentUser ( ) {
309
+ createOrGetUser { _ in }
306
310
}
307
311
308
312
private func getProducts( callback: @escaping ( ( [ String : String ] ? ) -> Void ) ) {
@@ -340,7 +344,7 @@ final class ApphudInternal {
340
344
internal func submitReceipt( productId : String , callback : ( ( ApphudSubscription ? , Error ? ) -> Void ) ? ) {
341
345
guard let receiptString = receiptDataString ( ) else {
342
346
ApphudStoreKitWrapper . shared. refreshReceipt ( )
343
- callback ? ( nil , nil )
347
+ callback ? ( nil , ApphudError . error ( message : " Receipt not found on device, refreshing. " ) )
344
348
return
345
349
}
346
350
@@ -350,7 +354,7 @@ final class ApphudInternal {
350
354
}
351
355
}
352
356
if !exist {
353
- apphudLog ( " Tried to make submitPurchase : \( productId) request when user is not yet registered, addind to schedule.. " )
357
+ apphudLog ( " Tried to make submitReceipt : \( productId) request when user is not yet registered, addind to schedule.. " )
354
358
}
355
359
}
356
360
@@ -360,7 +364,7 @@ final class ApphudInternal {
360
364
apphudLog ( " App Store receipt is missing on device, will refresh first then retry " )
361
365
ApphudStoreKitWrapper . shared. refreshReceipt ( )
362
366
} else {
363
- // receipt is missing and can 't refresh anymore because already tried to refresh
367
+ apphudLog ( " App Store receipt is missing on device and couldn 't be refreshed. " , forceDisplay : true )
364
368
}
365
369
return
366
370
}
@@ -425,7 +429,7 @@ final class ApphudInternal {
425
429
if let paymentDiscount = paymentDiscount {
426
430
ApphudInternal . shared. purchasePromo ( product: product, discount: paymentDiscount, callback: callback)
427
431
} else {
428
- // Signing error occurred, probably because you didn't add Subscription Key file to Apphud.
432
+ callback ? ( nil , ApphudError . error ( message : " Could not sign offer id: \( discountID ) , product id: \( product . productIdentifier ) " ) )
429
433
}
430
434
}
431
435
}
@@ -460,9 +464,8 @@ final class ApphudInternal {
460
464
}
461
465
}
462
466
463
- // let error = ApphudError("Could not sign promo offer: \(discountID)")
464
-
465
- callback ? ( nil , nil )
467
+ let error = ApphudError . error ( message: " Could not sign promo offer id: \( discountID) , product id: \( productID) " )
468
+ callback ? ( nil , error)
466
469
}
467
470
}
468
471
0 commit comments