Przeglądaj źródła

Inject flag to choose if product purchases should be atomic or not

Andrea Bizzotto 8 lat temu
rodzic
commit
f99147a2db

+ 2 - 2
SwiftyStoreKit-iOS-Demo/ViewController.swift

@@ -77,7 +77,7 @@ class ViewController: UIViewController {
     func purchase(_ purchase: RegisteredPurchase) {
         
         NetworkActivityIndicatorManager.networkOperationStarted()
-        SwiftyStoreKit.purchaseProduct(AppBundleId + "." + purchase.rawValue) { result in
+        SwiftyStoreKit.purchaseProduct(AppBundleId + "." + purchase.rawValue, atomically: true) { result in
             NetworkActivityIndicatorManager.networkOperationFinished()
             
             self.showAlert(self.alertForPurchaseResult(result))
@@ -86,7 +86,7 @@ class ViewController: UIViewController {
     @IBAction func restorePurchases() {
         
         NetworkActivityIndicatorManager.networkOperationStarted()
-        SwiftyStoreKit.restorePurchases() { results in
+        SwiftyStoreKit.restorePurchases(atomically: true) { results in
             NetworkActivityIndicatorManager.networkOperationFinished()
             
             self.showAlert(self.alertForRestorePurchases(results))

+ 7 - 5
SwiftyStoreKit/InAppProductPurchaseRequest.swift

@@ -42,26 +42,28 @@ class InAppProductPurchaseRequest: NSObject, SKPaymentTransactionObserver {
     }
     
     let product : SKProduct?
+    let atomically: Bool
     
     deinit {
         paymentQueue.remove(self)
     }
     // Initialiser for product purchase
-    private init(product: SKProduct?, callback: @escaping RequestCallback) {
+    private init(product: SKProduct?, atomically: Bool, callback: @escaping RequestCallback) {
 
+        self.atomically = atomically
         self.product = product
         self.callback = callback
         super.init()
         paymentQueue.add(self)
     }
     // MARK: Public methods
-    class func startPayment(_ product: SKProduct, applicationUsername: String = "", callback: @escaping RequestCallback) -> InAppProductPurchaseRequest {
-        let request = InAppProductPurchaseRequest(product: product, callback: callback)
+    class func startPayment(product: SKProduct, atomically: Bool, applicationUsername: String = "", callback: @escaping RequestCallback) -> InAppProductPurchaseRequest {
+        let request = InAppProductPurchaseRequest(product: product, atomically: atomically, callback: callback)
         request.startPayment(product, applicationUsername: applicationUsername)
         return request
     }
-    class func restorePurchases(_ callback: @escaping RequestCallback) -> InAppProductPurchaseRequest {
-        let request = InAppProductPurchaseRequest(product: nil, callback: callback)
+    class func restorePurchases(atomically: Bool, callback: @escaping RequestCallback) -> InAppProductPurchaseRequest {
+        let request = InAppProductPurchaseRequest(product: nil, atomically: atomically, callback: callback)
         request.startRestorePurchases()
         return request
     }

+ 8 - 7
SwiftyStoreKit/SwiftyStoreKit.swift

@@ -119,18 +119,19 @@ public class SwiftyStoreKit {
     /**
      *  Purchase a product
      *  - Parameter productId: productId as specified in iTunes Connect
+     *  - Parameter atomically: whether the product is purchased atomically (e.g. finishTransaction is called immediately)
      *  - Parameter applicationUsername: an opaque identifier for the user’s account on your system
      *  - Parameter completion: handler for result
      */
-    public class func purchaseProduct(_ productId: String, applicationUsername: String = "", completion: @escaping ( PurchaseResult) -> ()) {
+    public class func purchaseProduct(_ productId: String, atomically: Bool = true, applicationUsername: String = "", completion: @escaping ( PurchaseResult) -> ()) {
         
         if let product = sharedInstance.store.products[productId] {
-            sharedInstance.purchase(product: product, applicationUsername: applicationUsername, completion: completion)
+            sharedInstance.purchase(product: product, atomically: atomically, applicationUsername: applicationUsername, completion: completion)
         }
         else {
             retrieveProductsInfo(Set([productId])) { result -> () in
                 if let product = result.retrievedProducts.first {
-                    sharedInstance.purchase(product: product, applicationUsername: applicationUsername, completion: completion)
+                    sharedInstance.purchase(product: product, atomically: atomically, applicationUsername: applicationUsername, completion: completion)
                 }
                 else if let error = result.error {
                     completion(.error(error: .failed(error: error)))
@@ -142,9 +143,9 @@ public class SwiftyStoreKit {
         }
     }
     
-    public class func restorePurchases(_ completion: @escaping (RestoreResults) -> ()) {
+    public class func restorePurchases(atomically: Bool = true, completion: @escaping (RestoreResults) -> ()) {
 
-        sharedInstance.restoreRequest = InAppProductPurchaseRequest.restorePurchases() { results in
+        sharedInstance.restoreRequest = InAppProductPurchaseRequest.restorePurchases(atomically: atomically) { results in
         
             sharedInstance.restoreRequest = nil
             let results = sharedInstance.processRestoreResults(results)
@@ -235,7 +236,7 @@ public class SwiftyStoreKit {
     #endif
 
     // MARK: private methods
-    private func purchase(product: SKProduct, applicationUsername: String = "", completion: @escaping (PurchaseResult) -> ()) {
+    private func purchase(product: SKProduct, atomically: Bool, applicationUsername: String = "", completion: @escaping (PurchaseResult) -> ()) {
         guard SwiftyStoreKit.canMakePayments else {
             completion(.error(error: .paymentNotAllowed))
             return
@@ -245,7 +246,7 @@ public class SwiftyStoreKit {
             return
         }
 
-        inflightPurchases[productIdentifier] = InAppProductPurchaseRequest.startPayment(product, applicationUsername: applicationUsername) { results in
+        inflightPurchases[productIdentifier] = InAppProductPurchaseRequest.startPayment(product: product, atomically: atomically, applicationUsername: applicationUsername) { results in
 
             self.inflightPurchases[productIdentifier] = nil