Просмотр исходного кода

Updated alerts and making OS X demo target compile

Andrea Bizzotto 9 лет назад
Родитель
Сommit
dbf1f627e7
2 измененных файлов с 44 добавлено и 26 удалено
  1. 6 4
      SwiftyStoreDemo/ViewController.swift
  2. 38 22
      SwiftyStoreOSXDemo/ViewController.swift

+ 6 - 4
SwiftyStoreDemo/ViewController.swift

@@ -118,18 +118,20 @@ class ViewController: UIViewController {
             switch result {
             case .Success(let receipt):
               
+                let productId = self.AppBundleId + "." + purchase.rawValue
+                
                 // Specific behaviour for AutoRenewablePurchase
                 if purchase == .AutoRenewablePurchase {
                     let purchaseResult = SwiftyStoreKit.verifySubscription(
-                        productId: self.AppBundleId + "." + purchase.rawValue,
+                        productId: productId,
                         inReceipt: receipt,
                         validUntil: NSDate()
                     )
-                    self.showAlert(self.alertForVerifyPurchase(purchaseResult))
+                    self.showAlert(self.alertForVerifySubscription(purchaseResult))
                 }
                 else {
                     let purchaseResult = SwiftyStoreKit.verifyPurchase(
-                        productId: self.AppBundleId + "." + purchase.rawValue,
+                        productId: productId,
                         inReceipt: receipt
                     )
                     self.showAlert(self.alertForVerifyPurchase(purchaseResult))
@@ -246,7 +248,7 @@ extension ViewController {
         }
     }
   
-    func alertForVerifyPurchase(result: SwiftyStoreKit.verifySubscriptionResult) -> UIAlertController {
+    func alertForVerifySubscription(result: SwiftyStoreKit.verifySubscriptionResult) -> UIAlertController {
     
         switch result {
         case .Purchased(let expiresDate):

+ 38 - 22
SwiftyStoreOSXDemo/ViewController.swift

@@ -33,16 +33,8 @@ enum RegisteredPurchase : String {
     case NonConsumablePurchase = "nonConsumablePurchase"
     case ConsumablePurchase = "consumablePurchase"
     case AutoRenewablePurchase = "autoRenewablePurchase"
+    case NonRenewingPurchase = "nonRenewingPurchase"
     
-    var purchaseType: SwiftyStoreKit.PurchaseType {
-        switch self {
-        case .Purchase1: return .NonConsumable
-        case .Purchase2: return .NonConsumable
-        case .NonConsumablePurchase: return .NonConsumable
-        case .ConsumablePurchase: return .Consumable
-        case .AutoRenewablePurchase: return .AutomaticallyRenewableSubscription(validUntilDate: NSDate())
-        }
-    }
 }
 
 class ViewController: NSViewController {
@@ -115,13 +107,25 @@ class ViewController: NSViewController {
             switch result {
             case .Success(let receipt):
                 
-                let purchaseResult = SwiftyStoreKit.verifyPurchase(
-                    productId: self.AppBundleId + "." + purchase.rawValue,
-                    inReceipt: receipt,
-                    purchaseType: purchase.purchaseType
-                )
-                self.showAlert(self.alertForVerifyPurchase(purchaseResult))
+                let productId = self.AppBundleId + "." + purchase.rawValue
                 
+                // Specific behaviour for AutoRenewablePurchase
+                if purchase == .AutoRenewablePurchase {
+                    let purchaseResult = SwiftyStoreKit.verifySubscription(
+                        productId: productId,
+                        inReceipt: receipt,
+                        validUntil: NSDate()
+                    )
+                    self.showAlert(self.alertForVerifySubscription(purchaseResult))
+                }
+                else {
+                    let purchaseResult = SwiftyStoreKit.verifyPurchase(
+                        productId: productId,
+                        inReceipt: receipt
+                    )
+                    self.showAlert(self.alertForVerifyPurchase(purchaseResult))
+                }
+                            
             case .Error(_):
                 self.showAlert(self.alertForVerifyReceipt(result))
             }
@@ -221,21 +225,33 @@ extension ViewController {
             return self.alertWithTitle("Receipt verification failed", message: "The application will exit to create receipt data. You must have signed the application with your developer id to test and be outside of XCode")
         }
     }
-
-    func alertForVerifyPurchase(result: SwiftyStoreKit.VerifyPurchaseResult) -> NSAlert {
+    
+    func alertForVerifySubscription(result: SwiftyStoreKit.verifySubscriptionResult) -> NSAlert {
         
         switch result {
         case .Purchased(let expiresDate):
-            if let expiresDate = expiresDate {
-                return alertWithTitle("Product is purchased", message: "Product is valid until \(expiresDate)")
-            }
-            return alertWithTitle("Product is purchased", message: "Product will not expire")
-        case .Expired(let expiresDate): // Only for Automatically Renewable Subscription
+            print("Product is valid until \(expiresDate)")
+            return alertWithTitle("Product is purchased", message: "Product is valid until \(expiresDate)")
+        case .Expired(let expiresDate):
+            print("Product is expired since \(expiresDate)")
             return alertWithTitle("Product expired", message: "Product is expired since \(expiresDate)")
         case .NotPurchased:
+            print("This product has never been purchased")
             return alertWithTitle("Not purchased", message: "This product has never been purchased")
         }
     }
 
+
+    func alertForVerifyPurchase(result: SwiftyStoreKit.VerifyPurchaseResult) -> NSAlert {
+        
+        switch result {
+        case .Purchased:
+            print("Product is purchased")
+            return alertWithTitle("Product is purchased", message: "Product will not expire")
+        case .NotPurchased:
+            print("This product has never been purchased")
+            return alertWithTitle("Not purchased", message: "This product has never been purchased")
+        }
+    }
 }