Skip to content

Commit d764e89

Browse files
committed
Adds support for new screen constructor. 0.6.1 release
1 parent a3868d1 commit d764e89

16 files changed

+285
-129
lines changed

ApphudSDK.podspec

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
Pod::Spec.new do |s|
22
s.name = 'ApphudSDK'
3-
s.version = '0.6.0'
3+
s.version = '0.6.1'
44
s.summary = 'Track and control iOS auto-renewable subscriptions.'
55

66
s.description = 'Track, control and analyze iOS auto-renewable subscriptions with Apphud.'

Source/Apphud.swift

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,7 @@ final public class Apphud: NSObject {
9393
/**
9494
Purchases product and automatically submits App Store Receipt to Apphud.
9595

96-
__Note__: This method automatically sends in-app purchase receipt to Apphud, so you don't need to call `submitPurchase` method.
96+
__Note__: This method automatically sends in-app purchase receipt to Apphud, so you don't need to call `submitReceipt` method.
9797

9898
- parameter product: Required. This is an `SKProduct` object that user wants to purchase.
9999
- parameter callback: Optional. Returns `ApphudSubscription` object if succeeded and an optional error otherwise.
@@ -105,7 +105,7 @@ final public class Apphud: NSObject {
105105
/**
106106
Purchases promotional offer and automatically submits App Store Receipt to Apphud.
107107

108-
__Note__: This method automatically sends in-app purchase receipt to Apphud, so you don't need to call `submitPurchase` method.
108+
__Note__: This method automatically sends in-app purchase receipt to Apphud, so you don't need to call `submitReceipt` method.
109109

110110
- parameter product: Required. This is an `SKProduct` object that user wants to purchase.
111111
- parameter discountID: Required. This is a `SKProductDiscount` Identifier String object that you would like to apply.
@@ -189,7 +189,7 @@ final public class Apphud: NSObject {
189189
Example:
190190

191191
````
192-
Apphud.purchasedSubscription().isActive
192+
let active = Apphud.purchasedSubscription()?.isActive ?? false
193193
````
194194

195195
If you have more than one subscription group in your app, use `subscriptions()` method and get `isActive` value for your desired subscription.
@@ -235,16 +235,25 @@ final public class Apphud: NSObject {
235235
ApphudInternal.shared.submitAppStoreReceipt(allowsReceiptRefresh: true)
236236
}
237237

238+
/**
239+
Submit device push token to Apphud.
240+
- parameter token: Push token in Data class.
241+
- parameter callback: Returns true if successfully sent.
242+
*/
238243
@objc public static func submitPushNotificationsToken(token: Data, callback: @escaping (Bool) -> Void){
239244
ApphudInternal.shared.submitPushNotificationsToken(token: token, callback: callback)
240245
}
241246

242-
/*
247+
/**
248+
Handles push notification payload. Apphud handles only push notifications that were created by Apphud.
249+
- parameter apsInfo: Payload of push notification.
243250

251+
Returns true if push notification was handled by Apphud.
244252
*/
245253
@discardableResult @objc public static func handlePushNotification(apsInfo: [AnyHashable : Any]) -> Bool{
246254
return ApphudNotificationsHandler.shared.handleNotification(apsInfo)
247255
}
256+
248257
/**
249258
Enables debug logs. Better to call this method before SDK initialization.
250259
*/

Source/ApphudExtensions.swift

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,16 @@ internal func apphudLog(_ text : String, forceDisplay: Bool = false) {
2121
}
2222
}
2323

24+
enum ApphudError: LocalizedError {
25+
case error(message: String)
26+
var errorDescription: String? {
27+
switch self {
28+
case let .error(message):
29+
return message
30+
}
31+
}
32+
}
33+
2434
internal func apphudVisibleViewController() -> UIViewController? {
2535
var currentVC = UIApplication.shared.keyWindow?.rootViewController
2636
while let presentedVC = currentVC?.presentedViewController {

Source/ApphudHttpClient.swift

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,10 @@ public class ApphudHttpClient {
4343
}
4444

4545
internal func makeScreenRequest(screenID: String) -> URLRequest? {
46-
let urlString = "\(domain_url_string)/preview_screen/\(screenID)?api_key=\(apiKey)"
46+
47+
let deviceID : String = ApphudInternal.shared.currentDeviceID
48+
let urlString = "\(domain_url_string)/preview_screen/\(screenID)?api_key=\(apiKey)&locale=\(Locale.current.identifier)&device_id=\(deviceID)"
49+
4750
let url = URL(string: urlString)
4851
if url != nil {
4952
let request = URLRequest(url: url!, cachePolicy: URLRequest.CachePolicy.useProtocolCachePolicy, timeoutInterval: 20)

Source/ApphudInquiryController.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -86,9 +86,9 @@ internal class ApphudInquiryController: UIViewController {
8686

8787
let dismissButton = UIButton(type: .system)
8888
dismissButton.setTitleColor(UIColor(red: 0.04, green: 0.52, blue: 1, alpha: 1), for: .normal)
89-
dismissButton.titleLabel?.font = UIFont.systemFont(ofSize: 13)
89+
dismissButton.titleLabel?.font = UIFont.boldSystemFont(ofSize: 18)
9090
dismissButton.addTarget(self, action: #selector(dismissTapped), for: .touchUpInside)
91-
dismissButton.setTitle("Dismiss", for: .normal)
91+
dismissButton.setTitle("", for: .normal)
9292
self.view.addSubview(dismissButton)
9393
dismissButton.translatesAutoresizingMaskIntoConstraints = false
9494
NSLayoutConstraint.activate([

Source/ApphudInternal.swift

Lines changed: 39 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ import Foundation
1010
import AdSupport
1111
import StoreKit
1212

13-
let sdk_version = "0.6.0"
13+
let sdk_version = "0.6.1"
1414

1515
final class ApphudInternal {
1616

@@ -72,34 +72,14 @@ final class ApphudInternal {
7272
}
7373

7474
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 {
8877
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()
9679
self.continueToUpdateProducts()
97-
9880
self.listenForAwakeNotification()
9981
self.checkForUnreadNotifications()
100-
101-
} else {
102-
apphudLog("Failed to register user, error:\(error?.localizedDescription ?? "")")
82+
} else {
10383
self.userRegisteredCallbacks.removeAll()
10484
}
10585
}
@@ -152,6 +132,7 @@ final class ApphudInternal {
152132

153133
if Date().timeIntervalSince(lastCheckDate) > minCheckInterval{
154134
self.checkForUnreadNotifications()
135+
self.refreshCurrentUser()
155136
}
156137
}
157138

@@ -245,7 +226,7 @@ final class ApphudInternal {
245226

246227
// MARK: API Requests
247228

248-
private func registerUser(callback: @escaping ApphudBoolDictionaryCallback) {
229+
private func createOrGetUser(callback: @escaping (Bool) -> Void) {
249230

250231
var params : [String : String] = ["device_id" : self.currentDeviceID]
251232
if self.currentUserID != nil {
@@ -255,7 +236,30 @@ final class ApphudInternal {
255236
let deviceParams = currentDeviceParameters()
256237
params.merge(deviceParams) { (current, new) in current}
257238

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+
}
259263
}
260264

261265
private func updateUserCurrencyIfNeeded(priceLocale : Locale?){
@@ -301,8 +305,8 @@ final class ApphudInternal {
301305
httpClient.startRequest(path: "customers", params: params, method: .post, callback: callback)
302306
}
303307

304-
private func getCurrentUser(){
305-
308+
private func refreshCurrentUser(){
309+
createOrGetUser { _ in }
306310
}
307311

308312
private func getProducts(callback: @escaping (([String : String]?) -> Void)) {
@@ -340,7 +344,7 @@ final class ApphudInternal {
340344
internal func submitReceipt(productId : String, callback : ((ApphudSubscription?, Error?) -> Void)?) {
341345
guard let receiptString = receiptDataString() else {
342346
ApphudStoreKitWrapper.shared.refreshReceipt()
343-
callback?(nil, nil)
347+
callback?(nil, ApphudError.error(message: "Receipt not found on device, refreshing."))
344348
return
345349
}
346350

@@ -350,7 +354,7 @@ final class ApphudInternal {
350354
}
351355
}
352356
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..")
354358
}
355359
}
356360

@@ -360,7 +364,7 @@ final class ApphudInternal {
360364
apphudLog("App Store receipt is missing on device, will refresh first then retry")
361365
ApphudStoreKitWrapper.shared.refreshReceipt()
362366
} 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)
364368
}
365369
return
366370
}
@@ -425,7 +429,7 @@ final class ApphudInternal {
425429
if let paymentDiscount = paymentDiscount {
426430
ApphudInternal.shared.purchasePromo(product: product, discount: paymentDiscount, callback: callback)
427431
} 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)"))
429433
}
430434
}
431435
}
@@ -460,9 +464,8 @@ final class ApphudInternal {
460464
}
461465
}
462466

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)
466469
}
467470
}
468471

Source/ApphudScreen.swift

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,19 +12,17 @@ struct ApphudScreen {
1212

1313
var identifier: String
1414
var name: String
15-
var product_id: String
16-
var offer_id: String?
1715
var terms_url: String?
1816
var privacy_url: String?
1917
var status_bar_color: String?
18+
var products_offers_map : [[String : Any]]?
2019

2120
init(dictionary: [String : Any]) {
22-
product_id = dictionary["product_id"] as? String ?? ""
2321
name = dictionary["name"] as? String ?? ""
2422
identifier = dictionary["identifier"] as? String ?? ""
25-
offer_id = dictionary["offer_id"] as? String ?? ""
2623
terms_url = dictionary["terms_url"] as? String ?? ""
2724
privacy_url = dictionary["privacy_url"] as? String ?? ""
2825
status_bar_color = dictionary["status_bar_color"] as? String ?? ""
26+
products_offers_map = dictionary["products"] as? [[String : Any]]
2927
}
3028
}

0 commit comments

Comments
 (0)