Browse Source

Convert SwiftyStoreOSXDemo to Swift 3.0

Andrea Bizzotto 9 years ago
parent
commit
afa4cdacd2

+ 3 - 0
SwiftyStoreKit.xcodeproj/project.pbxproj

@@ -395,6 +395,7 @@
 					};
 					C4FD3A001C2954C10035CFF3 = {
 						CreatedOnToolsVersion = 7.2;
+						LastSwiftMigration = 0800;
 					};
 				};
 			};
@@ -838,6 +839,7 @@
 				PRODUCT_BUNDLE_IDENTIFIER = com.musevisions.OSX.SwiftyStoreDemo;
 				PRODUCT_NAME = "$(TARGET_NAME)";
 				SDKROOT = macosx;
+				SWIFT_VERSION = 3.0;
 			};
 			name = Debug;
 		};
@@ -855,6 +857,7 @@
 				PRODUCT_NAME = "$(TARGET_NAME)";
 				SDKROOT = macosx;
 				SWIFT_OPTIMIZATION_LEVEL = "-Owholemodule";
+				SWIFT_VERSION = 3.0;
 			};
 			name = Release;
 		};

+ 1 - 1
SwiftyStoreKit/InAppReceipt.swift

@@ -141,7 +141,7 @@ public enum ReceiptInfoField: String {
 #if os(OSX)
     public enum ReceiptExitCode: Int32 {
         // If validation fails in OS X, call exit with a status of 173. This exit status notifies the system that your application has determined that its receipt is invalid. At this point, the system attempts to obtain a valid receipt and may prompt for the user’s iTunes credentials
-        case NotValid = 173
+        case notValid = 173
     }
 #endif
 

+ 1 - 1
SwiftyStoreKit/SwiftyStoreKit.swift

@@ -222,7 +222,7 @@ public class SwiftyStoreKit {
     #elseif os(OSX)
      // Call exit with a status of 173. This exit status notifies the system that your application has determined that its receipt is invalid. At this point, the system attempts to obtain a valid receipt and may prompt for the user’s iTunes credentials
     public class func refreshReceipt() {
-         exit(ReceiptExitCode.NotValid.rawValue)
+         exit(ReceiptExitCode.notValid.rawValue)
     }
     #endif
 

+ 2 - 2
SwiftyStoreOSXDemo/AppDelegate.swift

@@ -28,7 +28,7 @@ import SwiftyStoreKit
 @NSApplicationMain
 class AppDelegate: NSObject, NSApplicationDelegate {
 
-    func applicationDidFinishLaunching(aNotification: NSNotification) {
+    func applicationDidFinishLaunching(_ aNotification: Notification) {
 
         completeIAPTransactions()
     }
@@ -39,7 +39,7 @@ class AppDelegate: NSObject, NSApplicationDelegate {
             
             for completedTransaction in completedTransactions {
                 
-                if completedTransaction.transactionState == .Purchased || completedTransaction.transactionState == .Restored {
+                if completedTransaction.transactionState == .purchased || completedTransaction.transactionState == .restored {
                     
                     print("purchased: \(completedTransaction.productId)")
                 }

+ 39 - 39
SwiftyStoreOSXDemo/ViewController.swift

@@ -45,27 +45,27 @@ class ViewController: NSViewController {
     let Purchase2 = RegisteredPurchase.AutoRenewablePurchase
 
     // MARK: actions
-    @IBAction func getInfo1(sender: AnyObject?) {
+    @IBAction func getInfo1(_ sender: AnyObject?) {
         getInfo(Purchase1)
     }
-    @IBAction func purchase1(sender: AnyObject?) {
+    @IBAction func purchase1(_ sender: AnyObject?) {
         purchase(Purchase1)
     }
-    @IBAction func verifyPurchase1(sender: AnyObject?) {
+    @IBAction func verifyPurchase1(_ sender: AnyObject?) {
         verifyPurchase(Purchase1)
     }
 
-    @IBAction func getInfo2(sender: AnyObject?) {
+    @IBAction func getInfo2(_ sender: AnyObject?) {
         getInfo(Purchase2)
     }
-    @IBAction func purchase2(sender: AnyObject?) {
+    @IBAction func purchase2(_ sender: AnyObject?) {
         purchase(Purchase2)
     }
-    @IBAction func verifyPurchase2(sender: AnyObject?) {
+    @IBAction func verifyPurchase2(_ sender: AnyObject?) {
         verifyPurchase(Purchase2)
     }
 
-    func getInfo(purchase: RegisteredPurchase) {
+    func getInfo(_ purchase: RegisteredPurchase) {
 
         SwiftyStoreKit.retrieveProductsInfo([AppBundleId + "." + purchase.rawValue]) { result in
 
@@ -73,7 +73,7 @@ class ViewController: NSViewController {
         }
     }
 
-    func purchase(purchase: RegisteredPurchase) {
+    func purchase(_ purchase: RegisteredPurchase) {
 
         SwiftyStoreKit.purchaseProduct(AppBundleId + "." + purchase.rawValue) { result in
 
@@ -81,7 +81,7 @@ class ViewController: NSViewController {
         }
     }
 
-    @IBAction func restorePurchases(sender: AnyObject?) {
+    @IBAction func restorePurchases(_ sender: AnyObject?) {
 
         SwiftyStoreKit.restorePurchases() { results in
             
@@ -89,7 +89,7 @@ class ViewController: NSViewController {
         }
     }
 
-    @IBAction func verifyReceipt(sender: AnyObject?) {
+    @IBAction func verifyReceipt(_ sender: AnyObject?) {
 
         SwiftyStoreKit.verifyReceipt() { result in
 
@@ -100,12 +100,12 @@ class ViewController: NSViewController {
         }
     }
 
-    func verifyPurchase(purchase: RegisteredPurchase) {
+    func verifyPurchase(_ purchase: RegisteredPurchase) {
         
         SwiftyStoreKit.verifyReceipt() { result in
             
             switch result {
-            case .Success(let receipt):
+            case .success(let receipt):
                 
                 let productId = self.AppBundleId + "." + purchase.rawValue
                 
@@ -114,7 +114,7 @@ class ViewController: NSViewController {
                     let purchaseResult = SwiftyStoreKit.verifySubscription(
                         productId: productId,
                         inReceipt: receipt,
-                        validUntil: NSDate()
+                        validUntil: Date()
                     )
                     self.showAlert(self.alertForVerifySubscription(purchaseResult))
                 }
@@ -126,7 +126,7 @@ class ViewController: NSViewController {
                     self.showAlert(self.alertForVerifyPurchase(purchaseResult))
                 }
                             
-            case .Error(_):
+            case .error(_):
                 self.showAlert(self.alertForVerifyReceipt(result))
             }
         }
@@ -139,18 +139,18 @@ class ViewController: NSViewController {
 // MARK: User facing alerts
 extension ViewController {
     
-    func alertWithTitle(title: String, message: String) -> NSAlert {
+    func alertWithTitle(_ title: String, message: String) -> NSAlert {
         
         let alert: NSAlert = NSAlert()
         alert.messageText = title
         alert.informativeText = message
-        alert.alertStyle = NSAlertStyle.InformationalAlertStyle
+        alert.alertStyle = NSAlertStyle.informational
         return alert
     }
-    func showAlert(alert: NSAlert, handler: ((NSModalResponse) -> Void)? = nil) {
+    func showAlert(_ alert: NSAlert, handler: ((NSModalResponse) -> Void)? = nil) {
         
-        if let window = NSApplication.sharedApplication().keyWindow {
-            alert.beginSheetModalForWindow(window)  { (response: NSModalResponse) in
+        if let window = NSApplication.shared().keyWindow {
+            alert.beginSheetModal(for: window)  { (response: NSModalResponse) in
                 handler?(response)
             }
         } else {
@@ -159,10 +159,10 @@ extension ViewController {
         }
     }
 
-    func alertForProductRetrievalInfo(result: SwiftyStoreKit.RetrieveResults) -> NSAlert {
+    func alertForProductRetrievalInfo(_ result: SwiftyStoreKit.RetrieveResults) -> NSAlert {
         
         if let product = result.retrievedProducts.first {
-            let priceString = NSNumberFormatter.localizedStringFromNumber(product.price ?? 0, numberStyle: .CurrencyStyle)
+            let priceString = NumberFormatter.localizedString(from: product.price ?? 0, number: .currency)
             return alertWithTitle(product.localizedTitle ?? "no title", message: "\(product.localizedDescription) - \(priceString)")
         }
         else if let invalidProductId = result.invalidProductIDs.first {
@@ -174,31 +174,31 @@ extension ViewController {
         }
     }
     
-    func alertForPurchaseResult(result: SwiftyStoreKit.PurchaseResult) -> NSAlert {
+    func alertForPurchaseResult(_ result: SwiftyStoreKit.PurchaseResult) -> NSAlert {
 
         switch result {
-        case .Success(let productId):
+        case .success(let productId):
             print("Purchase Success: \(productId)")
             return alertWithTitle("Thank You", message: "Purchase completed")
-        case .Error(let error):
+        case .error(let error):
             print("Purchase Failed: \(error)")
             switch error {
-            case .Failed(let error):
+            case .failed(let error):
                 if error.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 .InvalidProductId(let productId):
+            case .invalidProductId(let productId):
                 return alertWithTitle("Purchase failed", message: "\(productId) is not a valid product identifier")
-            case .NoProductIdentifier:
+            case .noProductIdentifier:
                 return alertWithTitle("Purchase failed", message: "Product not found")
-            case .PaymentNotAllowed:
+            case .paymentNotAllowed:
                 return alertWithTitle("Payments not enabled", message: "You are not allowed to make payments")
             }
         }
     }
     
-    func alertForRestorePurchases(results: SwiftyStoreKit.RestoreResults) -> NSAlert {
+    func alertForRestorePurchases(_ results: SwiftyStoreKit.RestoreResults) -> NSAlert {
         
         if results.restoreFailedProducts.count > 0 {
             print("Restore Failed: \(results.restoreFailedProducts)")
@@ -214,41 +214,41 @@ extension ViewController {
         }
     }
     
-    func alertForVerifyReceipt(result: SwiftyStoreKit.VerifyReceiptResult) -> NSAlert {
+    func alertForVerifyReceipt(_ result: SwiftyStoreKit.VerifyReceiptResult) -> NSAlert {
 
         switch result {
-        case .Success(let receipt):
+        case .success(let receipt):
             print("Verify receipt Success: \(receipt)")
             return self.alertWithTitle("Receipt verified", message: "Receipt verified remotly")
-        case .Error(let error):
+        case .error(let error):
             print("Verify receipt Failed: \(error)")
             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 alertForVerifySubscription(result: SwiftyStoreKit.VerifySubscriptionResult) -> NSAlert {
+    func alertForVerifySubscription(_ result: SwiftyStoreKit.VerifySubscriptionResult) -> NSAlert {
         
         switch result {
-        case .Purchased(let expiresDate):
+        case .purchased(let expiresDate):
             print("Product is valid until \(expiresDate)")
             return alertWithTitle("Product is purchased", message: "Product is valid until \(expiresDate)")
-        case .Expired(let expiresDate):
+        case .expired(let expiresDate):
             print("Product is expired since \(expiresDate)")
             return alertWithTitle("Product expired", message: "Product is expired since \(expiresDate)")
-        case .NotPurchased:
+        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 {
+    func alertForVerifyPurchase(_ result: SwiftyStoreKit.VerifyPurchaseResult) -> NSAlert {
         
         switch result {
-        case .Purchased:
+        case .purchased:
             print("Product is purchased")
             return alertWithTitle("Product is purchased", message: "Product will not expire")
-        case .NotPurchased:
+        case .notPurchased:
             print("This product has never been purchased")
             return alertWithTitle("Not purchased", message: "This product has never been purchased")
         }