From 57d1125e8680032c2bba0760894ef4afc1481879 Mon Sep 17 00:00:00 2001 From: Andrea Bizzotto Date: Mon, 24 Apr 2017 16:10:09 +0100 Subject: [PATCH 1/2] Rename expiresDate to expiryDate in demo apps --- SwiftyStoreKit-iOS-Demo/ViewController.swift | 12 ++++++------ SwiftyStoreKit-macOS-Demo/ViewController.swift | 12 ++++++------ 2 files changed, 12 insertions(+), 12 deletions(-) diff --git a/SwiftyStoreKit-iOS-Demo/ViewController.swift b/SwiftyStoreKit-iOS-Demo/ViewController.swift index 576aebe6..6c8f97e8 100644 --- a/SwiftyStoreKit-iOS-Demo/ViewController.swift +++ b/SwiftyStoreKit-iOS-Demo/ViewController.swift @@ -277,12 +277,12 @@ extension ViewController { func alertForVerifySubscription(_ result: VerifySubscriptionResult) -> UIAlertController { switch result { - 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): - print("Product is expired since \(expiresDate)") - return alertWithTitle("Product expired", message: "Product is expired since \(expiresDate)") + case .purchased(let expiryDate): + print("Product is valid until \(expiryDate)") + return alertWithTitle("Product is purchased", message: "Product is valid until \(expiryDate)") + case .expired(let expiryDate): + print("Product is expired since \(expiryDate)") + return alertWithTitle("Product expired", message: "Product is expired since \(expiryDate)") case .notPurchased: print("This product has never been purchased") return alertWithTitle("Not purchased", message: "This product has never been purchased") diff --git a/SwiftyStoreKit-macOS-Demo/ViewController.swift b/SwiftyStoreKit-macOS-Demo/ViewController.swift index 256fea8b..096db017 100644 --- a/SwiftyStoreKit-macOS-Demo/ViewController.swift +++ b/SwiftyStoreKit-macOS-Demo/ViewController.swift @@ -254,12 +254,12 @@ extension ViewController { func alertForVerifySubscription(_ result: VerifySubscriptionResult) -> NSAlert { switch result { - 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): - print("Product is expired since \(expiresDate)") - return alertWithTitle("Product expired", message: "Product is expired since \(expiresDate)") + case .purchased(let expiryDate): + print("Product is valid until \(expiryDate)") + return alertWithTitle("Product is purchased", message: "Product is valid until \(expiryDate)") + case .expired(let expiryDate): + print("Product is expired since \(expiryDate)") + return alertWithTitle("Product expired", message: "Product is expired since \(expiryDate)") case .notPurchased: print("This product has never been purchased") return alertWithTitle("Not purchased", message: "This product has never been purchased") From 44de0ed06db97e04ef790fa3e3619c41d319aba9 Mon Sep 17 00:00:00 2001 From: Andrea Bizzotto Date: Mon, 24 Apr 2017 16:13:30 +0100 Subject: [PATCH 2/2] Update Verify Subscription section in README (#174) --- README.md | 52 +++++++++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 51 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index 10dbbfa3..ccac2260 100644 --- a/README.md +++ b/README.md @@ -245,6 +245,10 @@ Note that for consumable products, the receipt will only include the information ### Verify Subscription +This can be used to check if a subscription was previously purchased, and whether it is still active or if it's expired. + +If a subscription has been purchased multiple times, this method will return `.purchased` or `.expired` for the most recent one. + ```swift let appleValidator = AppleReceiptValidator(service: .production) SwiftyStoreKit.verifyReceipt(using: appleValidator, password: "your-shared-secret") { result in @@ -281,14 +285,60 @@ let purchaseResult = SwiftyStoreKit.verifySubscription( #### Non-Renewing ``` +// validDuration: time interval in seconds let purchaseResult = SwiftyStoreKit.verifySubscription( type: .nonRenewing(validDuration: 3600 * 24 * 30), productId: "com.musevisions.SwiftyStoreKit.Subscription", inReceipt: receipt) ``` +**Note**: When purchasing subscriptions in sandbox mode, the expiry dates are set just minutes after the purchase date for testing purposes. + +#### Purchasing and verifying a subscription + +The `verifySubscription` method can be used together with the `purchaseProduct` method to purchase a subscription and check its expiration date, like so: + +```swift +let productId = "your-product-id" +SwiftyStoreKit.purchaseProduct(productId, atomically: true) { result in + + if case .success(let product) = result { + // Deliver content from server, then: + if product.needsFinishTransaction { + SwiftyStoreKit.finishTransaction(product.transaction) + } + + let appleValidator = AppleReceiptValidator(service: .production) + SwiftyStoreKit.verifyReceipt(using: appleValidator, password: "your-shared-secret") { result in + + if case .success(let receipt) = result { + let purchaseResult = SwiftyStoreKit.verifySubscription( + type: .autoRenewable, + productId: productId, + inReceipt: receipt) + + switch purchaseResult { + case .purchased(let expiryDate): + print("Product is valid until \(expiryDate)") + case .expired(let expiryDate): + print("Product is expired since \(expiryDate)") + case .notPurchased: + print("This product has never been purchased") + } + + } else { + // receipt verification error + } + } + + } else { + // purchase error + } +} +``` + -**NOTE**: +## Notes The framework provides a simple block based API with robust error handling on top of the existing StoreKit framework. It does **NOT** persist in app purchases data locally. It is up to clients to do this with a storage solution of choice (i.e. NSUserDefaults, CoreData, Keychain). ## Installation