Coder Social home page Coder Social logo

yippeeapp / swiftystorekit Goto Github PK

View Code? Open in Web Editor NEW

This project forked from bizz84/swiftystorekit

0.0 3.0 0.0 1.33 MB

Lightweight In App Purchases Swift framework for iOS 8.0+ and OSX 9.0+

License: MIT License

Swift 93.87% Ruby 1.18% Objective-C 4.94%

swiftystorekit's Introduction

SwiftyStoreKit

SwiftyStoreKit is a lightweight In App Purchases framework for iOS 8.0+ and OSX 9.0+, written in Swift.

License Platform Language Issues Cocoapod Carthage compatible

Preview

Retrieve product info

SwiftyStoreKit.retrieveProductInfo("com.musevisions.SwiftyStoreKit.Purchase1") { result in
    switch result {
    case .Success(let product):
        let priceString = NSNumberFormatter.localizedStringFromNumber(product.price, numberStyle: .CurrencyStyle)
        print("Product: \(product.localizedDescription), price: \(priceString)")
        break
    case .Error(let error):
        print("Error: \(error)")
        break
    }
}

Purchase a product

SwiftyStoreKit.purchaseProduct("com.musevisions.SwiftyStoreKit.Purchase1") { result in
    switch result {
    case .Success(let productId):
        print("Purchase Success: \(productId)")
        break
    case .Error(let error):
        print("Purchase Failed: \(error)")
        break
    }
}

Restore previous purchases

SwiftyStoreKit.restorePurchases() { result in
    switch result {
    case .Success(let productId):
        print("Restore Success: \(productId)")
        break
    case .NothingToRestore:
        print("Nothing to Restore")
        break
    case .Error(let error):
        print("Restore Failed: \(error)")
        break
    }
}

Verify Receipts

SwiftyStoreKit.verifyReceipt() { result in
    if case .Error(let error) = result {
        if case .NoReceiptData = error {
            self.refreshReceipt()
        }
    }
}

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

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.

Sample Code

The project includes demo apps for iOS and OSX 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
  • enum-based error handling
  • Support for non-consumable in app purchases
  • Receipt verification

Missing Features

  • Ask To Buy

Untested Features

  • Consumable in app purchases
  • Free subscriptions for Newsstand

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 a product

SwiftyStoreKit wraps the delegate-based SKProductRequest API with a block based class named InAppProductQueryRequest, which returns either a success case with a list of valid products, or an error comprising the following cases:

public enum ResponseError : ErrorType {
    case InvalidProducts(invalidProductIdentifiers: [String])
    case NoProducts
    case RequestFailed(error: NSError)
}

If InAppProductQueryRequest returns an error, this is surfaced directly to the completion block of SwiftyStoreKit.purchaseProduct, so that the client can examine it and react accordingly. In case of success, the product is cached and the purchase can take place via the InAppProductPurchaseRequest class.

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 NothingToRestore
    case Failed(error: NSError)
}

The SwiftyStoreKit class can then map the returned TransactionResult into either a success or failure case and pass this back to the client. Note that along with the success and failure case, the result of a restore purchases operation also has a NothingToRestore case. This is so that the client can know that the operation returned, but no purchases were restored.

Credits

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

License

Copyright (c) 2015 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.

swiftystorekit's People

Contributors

bizz84 avatar phimage avatar albinekcom avatar darekxan avatar

Watchers

Chew Chit Siang avatar James Cloos avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.