瀏覽代碼

Updated demo view controllers to handle new PurchaseResultType error types.
Refactored alert presentation code into separate categories.

Andrea Bizzotto 9 年之前
父節點
當前提交
09a94b3f0c
共有 2 個文件被更改,包括 145 次插入111 次删除
  1. 75 58
      SwiftStoreOSXDemo/ViewController.swift
  2. 70 53
      SwiftyStoreDemo/ViewController.swift

+ 75 - 58
SwiftStoreOSXDemo/ViewController.swift

@@ -13,25 +13,7 @@ import SwiftyStoreKit
 class ViewController: NSViewController {
 class ViewController: NSViewController {
 
 
     let AppBundleId = "com.musevisions.OSX.SwiftyStoreKit"
     let AppBundleId = "com.musevisions.OSX.SwiftyStoreKit"
-
-    override func viewDidLoad() {
-        super.viewDidLoad()
-    }
-
-    func showMessage(title: String, message: String) {
-
-        let alert: NSAlert = NSAlert()
-        alert.messageText = title
-        alert.informativeText = message
-        alert.alertStyle = NSAlertStyle.InformationalAlertStyle
-        if let window = NSApplication.sharedApplication().keyWindow {
-            alert.beginSheetModalForWindow(window) { (response: NSModalResponse) in
-            }
-        } else {
-            alert.runModal()
-        }
-        return
-    }
+    
     // MARK: actions
     // MARK: actions
     @IBAction func getInfo1(sender: AnyObject?) {
     @IBAction func getInfo1(sender: AnyObject?) {
         getInfo("1")
         getInfo("1")
@@ -49,15 +31,7 @@ class ViewController: NSViewController {
 
 
         SwiftyStoreKit.retrieveProductInfo(AppBundleId + ".purchase" + no) { result in
         SwiftyStoreKit.retrieveProductInfo(AppBundleId + ".purchase" + no) { result in
 
 
-            switch result {
-            case .Success(let product):
-                let priceString = NSNumberFormatter.localizedStringFromNumber(product.price ?? 0, numberStyle: .CurrencyStyle)
-                self.showMessage(product.localizedTitle ?? "no title", message: "\(product.localizedDescription) - \(priceString)")
-                break
-            case .Error(let error):
-                self.showMessage("Could not retrieve product info", message: String(error))
-                break
-            }
+            self.showAlert(self.alertForProductRetrievalInfo(result))
         }
         }
     }
     }
 
 
@@ -65,44 +39,87 @@ class ViewController: NSViewController {
 
 
         SwiftyStoreKit.purchaseProduct(AppBundleId + ".purchase" + no) { result in
         SwiftyStoreKit.purchaseProduct(AppBundleId + ".purchase" + no) { result in
 
 
-            switch result {
-            case .Success(let productId):
-                self.showMessage("Thank You", message: "Purchase completed")
-                print("Purchase Success: \(productId)")
-                break
-            case .Error(let error):
-                if case ResponseError.RequestFailed(let internalError) = error where internalError.domain == SKErrorDomain {
-                    self.showMessage("Purchase failed", message: "Please check your Internet connection or try again later")
-                }
-                else if (error as NSError).domain == SKErrorDomain {
-                    self.showMessage("Purchase failed", message: "Please check your Internet connection or try again later")
-                }
-                else {
-                    self.showMessage("Purchase failed", message: "Unknown error. Please contact support")
-                }
-                print("Purchase Failed: \(error)")
-                break
-            }
+            self.showAlert(self.alertForPurchaseResult(result))
         }
         }
     }
     }
+    
     @IBAction func restorePurchases(sender: AnyObject?) {
     @IBAction func restorePurchases(sender: AnyObject?) {
 
 
         SwiftyStoreKit.restorePurchases() { result in
         SwiftyStoreKit.restorePurchases() { result in
-            switch result {
-            case .Success(let productId):
-                self.showMessage("Purchases Restored", message: "All purchases have been restored")
-                print("Restore Success: \(productId)")
-                break
-            case .NothingToRestore:
-                self.showMessage("Nothing to restore", message: "No previous purchases were found")
-                print("Nothing to Restore")
-                break
-            case .Error(let error):
-                print("Restore Failed: \(error)")
-                break
+            
+            self.showAlert(self.alertForRestorePurchases(result))
+        }
+    }
+
+}
+
+// MARK: User facing alerts
+extension ViewController {
+    
+    func alertWithTitle(title: String, message: String) -> NSAlert {
+        
+        let alert: NSAlert = NSAlert()
+        alert.messageText = title
+        alert.informativeText = message
+        alert.alertStyle = NSAlertStyle.InformationalAlertStyle
+        return alert
+    }
+    func showAlert(alert: NSAlert) {
+        
+        if let window = NSApplication.sharedApplication().keyWindow {
+            alert.beginSheetModalForWindow(window) { (response: NSModalResponse) in
             }
             }
+        } else {
+            alert.runModal()
+        }
+    }
+    
+    func alertForProductRetrievalInfo(result: SwiftyStoreKit.RetrieveResultType) -> NSAlert {
+        
+        switch result {
+        case .Success(let product):
+            let priceString = NSNumberFormatter.localizedStringFromNumber(product.price ?? 0, numberStyle: .CurrencyStyle)
+            return alertWithTitle(product.localizedTitle ?? "no title", message: "\(product.localizedDescription) - \(priceString)")
+        case .Error(let error):
+            return alertWithTitle("Could not retrieve product info", message: String(error))
         }
         }
     }
     }
+    
+    func alertForPurchaseResult(result: SwiftyStoreKit.PurchaseResultType) -> NSAlert {
 
 
+        switch result {
+        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")
+            }
+            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")
+        }
+    }
+    
+    func alertForRestorePurchases(result: SwiftyStoreKit.RestoreResultType) -> NSAlert {
+        
+        switch result {
+        case .Success(let productId):
+            print("Restore Success: \(productId)")
+            return alertWithTitle("Purchases Restored", message: "All purchases have been restored")
+        case .NothingToRestore:
+            print("Nothing to Restore")
+            return alertWithTitle("Nothing to restore", message: "No previous purchases were found")
+        case .Error(let error):
+            print("Restore Failed: \(error)")
+            return alertWithTitle("Restore failed", message: "Unknown error. Please contact support")
+        }
+    }
 }
 }
 
 

+ 70 - 53
SwiftyStoreDemo/ViewController.swift

@@ -14,19 +14,6 @@ class ViewController: UIViewController {
 
 
     let AppBundleId = "com.musevisions.iOS.SwiftyStoreKit"
     let AppBundleId = "com.musevisions.iOS.SwiftyStoreKit"
     
     
-    override func viewDidLoad() {
-        super.viewDidLoad()
-    }
-    
-    func showMessage(title: String, message: String) {
-        
-        guard let _ = self.presentedViewController else {
-            let alert = UIAlertController(title: title, message: message, preferredStyle: .Alert)
-            alert.addAction(UIAlertAction(title: "OK", style: .Cancel, handler: nil))
-            self.presentViewController(alert, animated: true, completion: nil)
-            return
-        }
-    }
     // MARK: actions
     // MARK: actions
     @IBAction func getInfo1() {
     @IBAction func getInfo1() {
         getInfo("1")
         getInfo("1")
@@ -40,21 +27,14 @@ class ViewController: UIViewController {
     @IBAction func purchase2() {
     @IBAction func purchase2() {
         purchase("2")
         purchase("2")
     }
     }
+    
     func getInfo(no: String) {
     func getInfo(no: String) {
         
         
         NetworkActivityIndicatorManager.networkOperationStarted()
         NetworkActivityIndicatorManager.networkOperationStarted()
         SwiftyStoreKit.retrieveProductInfo(AppBundleId + ".purchase" + no) { result in
         SwiftyStoreKit.retrieveProductInfo(AppBundleId + ".purchase" + no) { result in
             NetworkActivityIndicatorManager.networkOperationFinished()
             NetworkActivityIndicatorManager.networkOperationFinished()
             
             
-            switch result {
-            case .Success(let product):
-                let priceString = NSNumberFormatter.localizedStringFromNumber(product.price, numberStyle: .CurrencyStyle)
-                self.showMessage(product.localizedTitle, message: "\(product.localizedDescription) - \(priceString)")
-                break
-            case .Error(let error):
-                self.showMessage("Could not retrieve product info", message: String(error))
-                break
-            }
+            self.showAlert(self.alertForProductRetrievalInfo(result))
         }
         }
     }
     }
     
     
@@ -64,24 +44,7 @@ class ViewController: UIViewController {
         SwiftyStoreKit.purchaseProduct(AppBundleId + ".purchase" + no) { result in
         SwiftyStoreKit.purchaseProduct(AppBundleId + ".purchase" + no) { result in
             NetworkActivityIndicatorManager.networkOperationFinished()
             NetworkActivityIndicatorManager.networkOperationFinished()
             
             
-            switch result {
-            case .Success(let productId):
-                self.showMessage("Thank You", message: "Purchase completed")
-                print("Purchase Success: \(productId)")
-                break
-            case .Error(let error):
-                if case ResponseError.RequestFailed(let internalError) = error where internalError.domain == SKErrorDomain {
-                    self.showMessage("Purchase failed", message: "Please check your Internet connection or try again later")
-                }
-                else if (error as NSError).domain == SKErrorDomain {
-                    self.showMessage("Purchase failed", message: "Please check your Internet connection or try again later")
-                }
-                else {
-                    self.showMessage("Purchase failed", message: "Unknown error. Please contact support")
-                }
-                print("Purchase Failed: \(error)")
-                break
-            }
+            self.showAlert(self.alertForPurchaseResult(result))
         }
         }
     }
     }
     @IBAction func restorePurchases() {
     @IBAction func restorePurchases() {
@@ -89,19 +52,8 @@ class ViewController: UIViewController {
         NetworkActivityIndicatorManager.networkOperationStarted()
         NetworkActivityIndicatorManager.networkOperationStarted()
         SwiftyStoreKit.restorePurchases() { result in
         SwiftyStoreKit.restorePurchases() { result in
             NetworkActivityIndicatorManager.networkOperationFinished()
             NetworkActivityIndicatorManager.networkOperationFinished()
-            switch result {
-            case .Success(let productId):
-                self.showMessage("Purchases Restored", message: "All purchases have been restored")
-                print("Restore Success: \(productId)")
-                break
-            case .NothingToRestore:
-                self.showMessage("Nothing to restore", message: "No previous purchases were found")
-                print("Nothing to Restore")
-                break
-            case .Error(let error):
-                print("Restore Failed: \(error)")
-                break
-            }
+            
+            self.showAlert(self.alertForRestorePurchases(result))
         }
         }
     }
     }
     
     
@@ -110,3 +62,68 @@ class ViewController: UIViewController {
     }
     }
 }
 }
 
 
+// MARK: User facing alerts
+extension ViewController {
+    
+    func alertWithTitle(title: String, message: String) -> UIAlertController {
+        
+        let alert = UIAlertController(title: title, message: message, preferredStyle: .Alert)
+        alert.addAction(UIAlertAction(title: "OK", style: .Cancel, handler: nil))
+        return alert
+    }
+    
+    func showAlert(alert: UIAlertController) {
+        guard let _ = self.presentedViewController else {
+            self.presentViewController(alert, animated: true, completion: nil)
+            return
+        }
+    }
+
+    func alertForProductRetrievalInfo(result: SwiftyStoreKit.RetrieveResultType) -> UIAlertController {
+        
+        switch result {
+        case .Success(let product):
+            let priceString = NSNumberFormatter.localizedStringFromNumber(product.price, numberStyle: .CurrencyStyle)
+            return alertWithTitle(product.localizedTitle, message: "\(product.localizedDescription) - \(priceString)")
+        case .Error(let error):
+            return alertWithTitle("Could not retrieve product info", message: String(error))
+        }
+    }
+
+    func alertForPurchaseResult(result: SwiftyStoreKit.PurchaseResultType) -> UIAlertController {
+        switch result {
+        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")
+            }
+            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")
+        }
+    }
+    
+    func alertForRestorePurchases(result: SwiftyStoreKit.RestoreResultType) -> UIAlertController {
+        
+        switch result {
+        case .Success(let productId):
+            print("Restore Success: \(productId)")
+            return alertWithTitle("Purchases Restored", message: "All purchases have been restored")
+        case .NothingToRestore:
+            print("Nothing to Restore")
+            return alertWithTitle("Nothing to restore", message: "No previous purchases were found")
+        case .Error(let error):
+            print("Restore Failed: \(error)")
+            return alertWithTitle("Restore failed", message: "Unknown error. Please contact support")
+        }
+    }
+}
+