Skip to content

Lightweight In App Purchases Swift framework for iOS 8.0+, tvOS 9.0+ and macOS 10.10+

License

Notifications You must be signed in to change notification settings

daveluong/SwiftyStoreKit

 
 

Repository files navigation

License Platform Language Build Issues Cocoapod Carthage compatible Twitter

SwiftyStoreKit is a lightweight In App Purchases framework for iOS 8.0+, tvOS 9.0+ and macOS 10.10+.

Language Branch Pod version Xcode version
Swift 3.0 master >= 0.5.x Xcode 8 or greater
Swift 2.3 swift-2.3 0.4.x Xcode 8, Xcode 7.3.x
Swift 2.2 swift-2.2 0.3.x Xcode 7.3.x

Preview

App startup

Complete Transactions

Apple recommends to register a transaction observer as soon as the app starts:

Adding your app's observer at launch ensures that it will persist during all launches of your app, thus allowing your app to receive all the payment queue notifications.

SwiftyStoreKit supports this by calling completeTransactions() when the app starts:

func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {

	SwiftyStoreKit.completeTransactions(atomically: true) { products in
	
	    for product in products {
	
	        if product.transaction.transactionState == .purchased || product.transaction.transactionState == .restored {
	
               if product.needsFinishTransaction {
                   // Deliver content from server, then:
                   SwiftyStoreKit.finishTransaction(product.transaction)
               }
               print("purchased: \(product)")
	        }
	    }
	}
  return true
}

If there are any pending transactions at this point, these will be reported by the completion block so that the app state and UI can be updated.

Purchases

Retrieve products info

SwiftyStoreKit.retrieveProductsInfo(["com.musevisions.SwiftyStoreKit.Purchase1"]) { result in
    if let product = result.retrievedProducts.first {
        let priceString = product.localizedPrice!
        print("Product: \(product.localizedDescription), price: \(priceString)")
    }
    else if let invalidProductId = result.invalidProductIDs.first {
        return alertWithTitle("Could not retrieve product info", message: "Invalid product identifier: \(invalidProductId)")
    }
    else {
	     print("Error: \(result.error)")
    }
}

Purchase a product

  • Atomic: to be used when the content is delivered immediately.
SwiftyStoreKit.purchaseProduct("com.musevisions.SwiftyStoreKit.Purchase1", atomically: true) { result in
    switch result {
    case .success(let product):
        print("Purchase Success: \(product.productId)")
    case .error(let error):
        print("Purchase Failed: \(error)")
    }
}
  • Non-Atomic: to be used when the content is delivered by the server.
SwiftyStoreKit.purchaseProduct("com.musevisions.SwiftyStoreKit.Purchase1", atomically: false) { result in
    switch result {
    case .success(let product):
        // fetch content from your server, then:
        if product.needsFinishTransaction {
            SwiftyStoreKit.finishTransaction(product.transaction)
        }
        print("Purchase Success: \(product.productId)")
    case .error(let error):
        print("Purchase Failed: \(error)")
    }
}

Restore previous purchases

  • Atomic: to be used when the content is delivered immediately.
SwiftyStoreKit.restorePurchases(atomically: true) { results in
    if results.restoreFailedProducts.count > 0 {
        print("Restore Failed: \(results.restoreFailedProducts)")
    }
    else if results.restoredProducts.count > 0 {
        print("Restore Success: \(results.restoredProducts)")
    }
    else {
        print("Nothing to Restore")
    }
}
  • Non-Atomic: to be used when the content is delivered by the server.
SwiftyStoreKit.restorePurchases(atomically: false) { results in
    if results.restoreFailedProducts.count > 0 {
        print("Restore Failed: \(results.restoreFailedProducts)")
    }
    else if results.restoredProducts.count > 0 {
        for product in results.restoredProducts {
            // fetch content from your server, then:
            if product.needsFinishTransaction {
                SwiftyStoreKit.finishTransaction(product.transaction)
            }
        }
        print("Restore Success: \(results.restoredProducts)")
    }
    else {
        print("Nothing to Restore")
    }
}

What does atomic / non-atomic mean?

When you purchase a product the following things happen:

  • A payment is added to the payment queue for your IAP.
  • When the payment has been processed with Apple, the payment queue is updated so that the appropriate transaction can be handled.
  • If the transaction state is purchased or restored, the app can unlock the functionality purchased by the user.
  • The app should call finishTransaction() to complete the purchase.

This is what is recommended by Apple:

Your application should call finishTransaction(_:) only after it has successfully processed the transaction and unlocked the functionality purchased by the user.

  • A purchase is atomic when the app unlocks the functionality purchased by the user immediately and call finishTransaction() at the same time. This is desirable if you're unlocking functionality that is already inside the app.

  • In cases when you need to make a request to your own server in order to unlock the functionality, you can use a non-atomic purchase instead.

SwiftyStoreKit provides three operations that can be performed atomically or non-atomically:

  • Making a purchase
  • Restoring purchases
  • Completing transactions on app launch

Receipt verification

Retrieve local receipt

let receiptData = SwiftyStoreKit.localReceiptData
let receiptString = receiptData.base64EncodedString(options: [])
// do your receipt validation here

Verify Receipt

SwiftyStoreKit.verifyReceipt(password: "your-shared-secret") { result in
    if case .error(let error) = result {
        if case .noReceiptData = error {
            self.refreshReceipt()
        }
    }
}

func refreshReceipt() {
    SwiftyStoreKit.refreshReceipt { result in
        switch result {
        case .success(let receiptData):
            print("Receipt refresh success: \(receiptData.base64EncodedString)")
        case .error(let error):
            print("Receipt refresh failed: \(error)")
        }
    }
}

Verify Purchase

SwiftyStoreKit.verifyReceipt(password: "your-shared-secret") { result in
    switch result {
    case .success(let receipt):
        // Verify the purchase of Consumable or NonConsumable
        let purchaseResult = SwiftyStoreKit.verifyPurchase(
            productId: "com.musevisions.SwiftyStoreKit.Purchase1",
            inReceipt: receipt
        )
        switch purchaseResult {
        case .purchased(let expiresDate):
            print("Product is purchased.")
        case .notPurchased:
            print("The user has never purchased this product")
        }
    case .Error(let error):
        print("Receipt verification failed: \(error)")
    }
}

Note that for consumable products, the receipt will only include the information for a couples of minutes after the purchase.

Verify Subscription

SwiftyStoreKit.verifyReceipt(password: "your-shared-secret") { result in
    switch result {
    case .success(let receipt):
        // Verify the purchase of a Subscription
        let purchaseResult = SwiftyStoreKit.verifySubscription(
            productId: "com.musevisions.SwiftyStoreKit.Subscription",
            inReceipt: receipt,
            validUntil: NSDate(),
            validDuration: 3600 * 24 * 30 // Non Renewing Subscription only
        )
        switch purchaseResult {
        case .purchased(let expiresDate):
            print("Product is valid until \(expiresDate)")
        case .expired(let expiresDate):
            print("Product is expired since \(expiresDate)")
        case .notPurchased:
            print("The user has never purchased this product")
        }

    case .error(let error):
        print("Receipt verification failed: \(error)")
    }
}

To test the expiration of a Non Renewing Subscription, you must indicate the validDuration time interval in seconds.

NOTE: 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

CocoaPods

SwiftyStoreKit can be installed as a CocoaPod and builds as a Swift framework. To install, include this in your Podfile.

use_frameworks!

pod 'SwiftyStoreKit'

Once installed, just import SwiftyStoreKit in your classes and you're good to go.

Carthage

To integrate SwiftyStoreKit into your Xcode project using Carthage, specify it in your Cartfile:

github "bizz84/SwiftyStoreKit"

NOTE: Please ensure that you have the latest Carthage installed.

Swift 2.2 / 2.3 / 3.0

Language Branch Pod version Xcode version
Swift 3.0 master >= 0.5.x Xcode 8 or greater
Swift 2.3 swift-2.3 0.4.x Xcode 8, Xcode 7.3.x
Swift 2.2 swift-2.2 0.3.x Xcode 7.3.x

Change Log

See the Releases Page

Sample Code

The project includes demo apps for iOS and macOS showing how to use SwiftyStoreKit. Note that the pre-registered in app purchases in the demo apps are for illustration purposes only and may not work as iTunes Connect may invalidate them.

Features

  • Super easy to use block based API
  • Support for consumable, non-consumable in-app purchases
  • Support for free, auto renewable and non renewing subscriptions
  • Receipt verification
  • iOS, tvOS and macOS compatible
  • enum-based error handling

Known issues

Requests lifecycle

While SwiftyStoreKit tries handle concurrent purchase or restore purchases requests, it is not guaranteed that this will always work flawlessly. This is in part because using a closure-based API does not map perfectly well with the lifecycle of payments in SKPaymentQueue.

In real applications the following could happen:

  1. User starts a purchase
  2. User kills the app
  3. OS continues processing this, resulting in a failed or successful purchase
  4. App is restarted (payment queue is not updated yet)
  5. User starts another purchase (the old transaction may interfere with the new purchase)

To prevent situations like this from happening, a completeTransactions() method has been added in version 0.2.8. This should be called when the app starts as it can take care of clearing the payment queue and notifying the app of the transactions that have finished.

Multiple accounts

The user can background the hosting application and change the Apple ID used with the App Store, then foreground the app. This has been observed to cause problems with SwiftyStoreKit - other IAP implementations may suffer from this as well.

Essential Reading

Implementation Details

In order to make a purchase, two operations are needed:

  • Obtain the SKProduct corresponding to the productId that identifies the app purchase, via SKProductRequest.

  • Submit the payment for that product via SKPaymentQueue.

The framework takes care of caching SKProducts so that future requests for the same SKProduct don't need to perform a new SKProductRequest.

Requesting products information

SwiftyStoreKit wraps the delegate-based SKProductRequest API with a block based class named InAppProductQueryRequest, which returns a RetrieveResults value with information about the obtained products:

public struct RetrieveResults {
    public let retrievedProducts: Set<SKProduct>
    public let invalidProductIDs: Set<String>
    public let error: NSError?
}

This value is then surfaced back to the caller of the retrieveProductsInfo() method the completion closure so that the client can update accordingly.

Purchasing a product / Restoring purchases

InAppProductPurchaseRequest is a wrapper class for SKPaymentQueue that can be used to purchase a product or restore purchases.

The class conforms to the SKPaymentTransactionObserver protocol in order to receive transactions notifications from the payment queue. The following outcomes are defined for a purchase/restore action:

enum TransactionResult {
    case purchased(productId: String)
    case restored(productId: String)
    case failed(error: NSError)
}

Depending on the operation, the completion closure for InAppProductPurchaseRequest is then mapped to either a PurchaseResult or a RestoreResults value and returned to the caller.

Credits

Many thanks to phimage for adding macOS support and receipt verification.

Apps using SwiftyStoreKit

It would be great to showcase apps using SwiftyStoreKit here. Pull requests welcome :)

License

Copyright (c) 2015-2016 Andrea Bizzotto [email protected]

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

About

Lightweight In App Purchases Swift framework for iOS 8.0+, tvOS 9.0+ and macOS 10.10+

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages

  • Swift 93.6%
  • Objective-C 5.2%
  • Other 1.2%