/
PurchasesDelegateHandler.swift
50 lines (39 loc) · 1.59 KB
/
PurchasesDelegateHandler.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
//
// PurchasesDelegateHandler.swift
// Magic Weather SwiftUI
//
// Created by Cody Kerns on 1/19/21.
//
import Foundation
import RevenueCat
/*
The class we'll use to publish CustomerInfo data to our Magic Weather app.
*/
class PurchasesDelegateHandler: NSObject, ObservableObject {
static let shared = PurchasesDelegateHandler()
}
extension PurchasesDelegateHandler: PurchasesDelegate {
/**
Whenever the `shared` instance of Purchases updates the CustomerInfo cache, this method will be called.
- Note: CustomerInfo is not pushed to each Purchases client, it has to be fetched.
This delegate method is only called when the SDK updates its cache after an app launch, purchase, restore, or fetch.
You still need to call `Purchases.shared.customerInfo` to fetch CustomerInfo regularly.
*/
func purchases(_ purchases: Purchases, receivedUpdated customerInfo: CustomerInfo) {
/// - Update our published customerInfo object
UserViewModel.shared.customerInfo = customerInfo
}
/**
- Note: this can be tested by opening a link like:
itms-services://?action=purchaseIntent&bundleId=<BUNDLE_ID>&productIdentifier=<SKPRODUCT_ID>
*/
func purchases(_ purchases: Purchases,
readyForPromotedProduct product: StoreProduct,
purchase startPurchase: @escaping StartPurchaseBlock) {
startPurchase { (transaction, info, error, cancelled) in
if let info = info, error == nil, !cancelled {
UserViewModel.shared.customerInfo = info
}
}
}
}