瀏覽代碼

Simplified PurchaseResultType to specify only one error case with internal PurchaseErrorType result

Andrea Bizzotto 9 年之前
父節點
當前提交
4be7066279
共有 3 個文件被更改,包括 36 次插入27 次删除
  1. 13 10
      SwiftyStoreDemo/ViewController.swift
  2. 10 7
      SwiftyStoreKit/SwiftyStoreKit.swift
  3. 13 10
      SwiftyStoreOSXDemo/ViewController.swift

+ 13 - 10
SwiftyStoreDemo/ViewController.swift

@@ -135,19 +135,22 @@ extension ViewController {
         case .Success(let productId):
             print("Purchase Success: \(productId)")
             return alertWithTitle("Thank You", message: "Purchase completed")
-        case .NoProductIdentifier:
-            return alertWithTitle("Purchase failed", message: "Product not found")
-        case .PaymentNotAllowed:
-            return alertWithTitle("Payments not enabled", message: "You are not allowed to make payments")
         case .Error(let error):
             print("Purchase Failed: \(error)")
-            if case ResponseError.RequestFailed(let internalError) = error where internalError.domain == SKErrorDomain {
-                return alertWithTitle("Purchase failed", message: "Please check your Internet connection or try again later")
+            switch error {
+                case .Failed(let error):
+                    if case ResponseError.RequestFailed(let internalError) = error where internalError.domain == SKErrorDomain {
+                        return alertWithTitle("Purchase failed", message: "Please check your Internet connection or try again later")
+                    }
+                    if (error as NSError).domain == SKErrorDomain {
+                        return alertWithTitle("Purchase failed", message: "Please check your Internet connection or try again later")
+                    }
+                    return alertWithTitle("Purchase failed", message: "Unknown error. Please contact support")
+                case .NoProductIdentifier:
+                    return alertWithTitle("Purchase failed", message: "Product not found")
+                case .PaymentNotAllowed:
+                    return alertWithTitle("Payments not enabled", message: "You are not allowed to make payments")
             }
-            if (error as NSError).domain == SKErrorDomain {
-                return alertWithTitle("Purchase failed", message: "Please check your Internet connection or try again later")
-            }
-            return alertWithTitle("Purchase failed", message: "Unknown error. Please contact support")
         }
     }
     

+ 10 - 7
SwiftyStoreKit/SwiftyStoreKit.swift

@@ -45,12 +45,15 @@ public class SwiftyStoreKit {
     private var receiptRefreshRequest: InAppReceiptRefreshRequest?
     #endif
     // MARK: Enums
-    public enum PurchaseResultType {
-        case Success(productId: String)
-        case Error(error: ErrorType)
+    public enum PurchaseErrorType {
+        case Failed(error: ErrorType)
         case NoProductIdentifier
         case PaymentNotAllowed
     }
+    public enum PurchaseResultType {
+        case Success(productId: String)
+        case Error(error: PurchaseErrorType)
+    }
     public enum RetrieveResultType {
         case Success(product: SKProduct)
         case Error(error: ErrorType)
@@ -102,7 +105,7 @@ public class SwiftyStoreKit {
                     sharedInstance.purchase(product: product, completion: completion)
                 }
                 else if case .Error(let error) = result {
-                    completion(result: .Error(error: error))
+                    completion(result: .Error(error: .Failed(error: error)))
                 }
             }
         }
@@ -164,11 +167,11 @@ public class SwiftyStoreKit {
     // MARK: private methods
     private func purchase(product product: SKProduct, completion: (result: PurchaseResultType) -> ()) {
         guard SwiftyStoreKit.canMakePayments else {
-            completion(result: PurchaseResultType.PaymentNotAllowed)
+            completion(result: .Error(error: .PaymentNotAllowed))
             return
         }
         guard let productIdentifier = product._productIdentifier else {
-            completion(result: PurchaseResultType.NoProductIdentifier)
+            completion(result: .Error(error: .NoProductIdentifier))
             return
         }
 
@@ -188,7 +191,7 @@ public class SwiftyStoreKit {
             // TODO: Need a way to match with current product?
             return .Success(productId: productId)
         case .Failed(let error):
-            return .Error(error: error)
+            return .Error(error: .Failed(error: error))
         case .Restored(_):
             fatalError("case Restored is not allowed for purchase flow")
         case .NothingToRestore:

+ 13 - 10
SwiftyStoreOSXDemo/ViewController.swift

@@ -121,19 +121,22 @@ extension ViewController {
         case .Success(let productId):
             print("Purchase Success: \(productId)")
             return alertWithTitle("Thank You", message: "Purchase completed")
-        case .NoProductIdentifier:
-            return alertWithTitle("Purchase failed", message: "Product not found")
-        case .PaymentNotAllowed:
-            return alertWithTitle("Payments not enabled", message: "You are not allowed to make payments")
         case .Error(let error):
             print("Purchase Failed: \(error)")
-            if case ResponseError.RequestFailed(let internalError) = error where internalError.domain == SKErrorDomain {
-                return alertWithTitle("Purchase failed", message: "Please check your Internet connection or try again later")
+            switch error {
+            case .Failed(let error):
+                if case ResponseError.RequestFailed(let internalError) = error where internalError.domain == SKErrorDomain {
+                    return alertWithTitle("Purchase failed", message: "Please check your Internet connection or try again later")
+                }
+                if (error as NSError).domain == SKErrorDomain {
+                    return alertWithTitle("Purchase failed", message: "Please check your Internet connection or try again later")
+                }
+                return alertWithTitle("Purchase failed", message: "Unknown error. Please contact support")
+            case .NoProductIdentifier:
+                return alertWithTitle("Purchase failed", message: "Product not found")
+            case .PaymentNotAllowed:
+                return alertWithTitle("Payments not enabled", message: "You are not allowed to make payments")
             }
-            if (error as NSError).domain == SKErrorDomain {
-                return alertWithTitle("Purchase failed", message: "Please check your Internet connection or try again later")
-            }
-            return alertWithTitle("Purchase failed", message: "Unknown error. Please contact support")
         }
     }