Browse Source

Merge pull request #572 from motocodeltd/add_support_for_entitlement_revocation

add support for ios14 entitlement revocation
Samuel Spencer 5 years ago
parent
commit
daddd07e53

+ 25 - 1
Sources/SwiftyStoreKit/PaymentQueueController.swift

@@ -88,6 +88,14 @@ extension SKPaymentTransactionState: CustomDebugStringConvertible {
     }
 }
 
+struct EntitlementRevocation {
+    let callback: ([String]) -> Void
+
+    init(callback: @escaping ([String]) -> Void) {
+        self.callback = callback
+    }
+}
+
 class PaymentQueueController: NSObject, SKPaymentTransactionObserver {
     
     private let paymentsController: PaymentsController
@@ -97,7 +105,9 @@ class PaymentQueueController: NSObject, SKPaymentTransactionObserver {
     private let completeTransactionsController: CompleteTransactionsController
     
     unowned let paymentQueue: PaymentQueue
-    
+
+    private var entitlementRevocation: EntitlementRevocation?
+
     deinit {
         paymentQueue.remove(self)
     }
@@ -145,6 +155,15 @@ class PaymentQueueController: NSObject, SKPaymentTransactionObserver {
         paymentsController.append(payment)
     }
     
+    func onEntitlementRevocation(_ revocation: EntitlementRevocation) {
+        guard entitlementRevocation == nil else {
+            print("SwiftyStoreKit.onEntitlementRevocation() should only be called once when the app launches. Ignoring this call")
+            return
+        }
+
+        self.entitlementRevocation = revocation
+    }
+
     func restorePurchases(_ restorePurchases: RestorePurchases) {
         assertCompleteTransactionsWasCalled()
         
@@ -233,6 +252,11 @@ class PaymentQueueController: NSObject, SKPaymentTransactionObserver {
         }
     }
     
+    func paymentQueue(_ queue: SKPaymentQueue, didRevokeEntitlementsForProductIdentifiers productIdentifiers: [String]) {
+
+        self.entitlementRevocation?.callback(productIdentifiers)
+    }
+    
     func paymentQueue(_ queue: SKPaymentQueue, removedTransactions transactions: [SKPaymentTransaction]) {
         
     }

+ 13 - 0
Sources/SwiftyStoreKit/SwiftyStoreKit.swift

@@ -84,6 +84,11 @@ public class SwiftyStoreKit {
         
         paymentQueueController.completeTransactions(CompleteTransactions(atomically: atomically, callback: completion))
     }
+
+    fileprivate func onEntitlementRevocation(completion: @escaping ([String]) -> Void) {
+
+        paymentQueueController.onEntitlementRevocation(EntitlementRevocation(callback: completion))
+    }
     
     fileprivate func finishTransaction(_ transaction: PaymentTransaction) {
         
@@ -187,6 +192,14 @@ extension SwiftyStoreKit {
         
         sharedInstance.completeTransactions(atomically: atomically, completion: completion)
     }
+
+    /// Entitlement revocation notification
+    ///   - Parameter completion: handler for result (list of product identifiers revoked)
+    @available(iOS 14, tvOS 14, OSX 11, watchOS 7, macCatalyst 14, *)
+    public class func onEntitlementRevocation(completion: @escaping ([String]) -> Void) {
+
+        sharedInstance.onEntitlementRevocation(completion: completion)
+    }
     
     /// Finish a transaction
     ///