A simple approach to Apple In App Purchases (IAP) that handles the presentation of products, purchasing, receipt validation, and timed free trials.
Switch branches/tags
Nothing to show
Clone or download
Latest commit 2589846 Jun 14, 2018
Permalink
Type Name Latest commit message Commit time
Failed to load latest commit information.
.github Add files Jun 13, 2018
Core Add files Jun 13, 2018
Examples Add files Jun 13, 2018
IAP.xcodeproj Remove tvOS references. Jun 13, 2018
ReadMeImages Add in Black Pixel information at bottom of readme along with badges … Jun 13, 2018
Resources Add files Jun 13, 2018
iOS Add files Jun 13, 2018
.gitignore Add files Jun 13, 2018
LICENSE Initial commit Jun 13, 2018
README.md Double up on <br /> to actually create spacing in github Jun 13, 2018
releaseNotes.md Update readme and release notes Jun 13, 2018

README.md

IAPKit

license PRs Welcome

A simple approach to Apple In App Purchases (IAP) that handles the presentation of products, purchasing, receipt validation, and timed free trials.

IAPKit is used by Kaleidoscope 2 and Pixelboard on iPad.

Requirements

  • An app running iOS 11+
  • StoreKit

Features

  • Supports SafeArea Layout
  • Supports Multitasking on iPad

Release Notes

Release notes

Examples

iPhone Portrait iPhone Landscape iPad Portrait iPad Split Screen

Installation

Simply grab the framework (either via git submodule or another package manager).

Add the IAP.xcodeproj to your Xcode project. Once that is complete, in your "Build Phases" add the IAP.framework to your "Link Binary with Libraries" phase.

Configuration

First thing is to define your app-specific product types by creating a new string typed enum that reference your IAP products in iTunes Connect. Be sure to import IAP and StoreKit.

enum IAPProducts: String {
    case freeTrial = "001"
    case standard = "002"
    case pro = "003"

    static let allIdentifiers: [IAPProducts] = [.freeTrial,
                                                 .standard,
                                                 .pro]
}

Then, extend your products to conform to Purchaseable

extension IAPProducts: Purchaseable {

    static var relevantProductIdentifiers: Set<ProductIdentifier> {
        return Set(IAPProducts.allIdentifiers.map{ $0.rawValue })
    }

    static var accentColorForStore: IAPColor {
        return UIColor(red:0.40, green:0.32, blue:0.81, alpha:1.00)
    }

    var lifetime: ProductLifetime {
        switch self {
        case .freeTrial: return .expiring(afterSeconds: Days(14))
        case .standard: return .unlimited
        case .pro: return .unlimited
        }
    }

    var productIdentifier: ProductIdentifier {
        return rawValue
    }

    var marketingMessage: String {
        switch self {
        case .freeTrial:
            return "Description of your free trial."
        case .standard:
            return "Description of your standard features."
        case .pro:
            return "Description of your pro features."
        }
    }

    var callToActionButtonText: String {
        switch self {
        case .freeTrial:
            return "Start Free Trial"
        case .standard:
            return "Buy Now"
        case .pro:
            return "Go Pro Now"
        }
    }

    var marketingTitle: String {
        switch self {
        case .freeTrial:
            return "Try [App Name] Now"
        case .standard:
            return "Buy [App Name] Now"
        case .pro:
            return "Go Pro Now"
        }
    }

    init?(productIdentifier: ProductIdentifier) {
        self.init(rawValue: productIdentifier)
    }
}


// Conveniences

fileprivate func Days(_ days: Int) -> TimeInterval {
    return TimeInterval(days * 24 * 60 * 60)
}

Last, extend your products to conform to Comparable (required by Purchaseable)

extension IAPProducts: Comparable {
    static func <(lhs: IAPProducts, rhs: IAPProducts) -> Bool {
        return lhs.sortIndex < rhs.sortIndex
    }

    private var sortIndex: Int {
        switch self {
        case .freeTrial: return 1
        case .standard: return 2
        case .pro: return 3
        }
    }
}

Usage

Import IAPKit

import IAP

Create a store object

private let store = Store<IAPProducts>()

Then, utilize the many options publicly available with the store object:

Check for purchased products

store.purchasedProducts // `[PurchasedProduct]?`

Check to see if purchases are available

store.hasPurchasesAvailable // `Bool`

Determine if any receipts are of type Availability.purchased

store.isAppPurchased // `Bool`

Restore purchases

store.restorePurchases()

Check for availability of a product

store.availability(for product: ) // -> `Availability`

Present available purchases

store.presentAvailablePurchases(from presentingViewController: completion:)

Store delegate methods

func didRestorePurchases()
func didCompletePurchase(productIdentifier: )
func transactionDidFail(localizedErrorMessage: )
func transactionCancelled()

OpenSSL

This framework has a dependency on OpenSSL. OpenSSL is required for receipt validation of the receipt delivered by Apple via the AppStore. Note: For the time being, the OpenSSL static libraries have only been incorporated into the iOS target.

The static library was built from the repo and instructions for OpenSSL-for-iPhone

The integration of the OpenSSL libraries vary somewhat from the instructions for OpenSSL-for-iPhone for the reason that their sample project shows how to integrate the library into a single target app. Because the BPXL IAP Framework is distributed as a "framework" we must follow a different procedure as we cannot import the OpenSSL headers in a bridging-header.h file. Instead we must integrate the library using a module.modulemap. You will find this file in iOS/ProjectModule/module.modulemap.

Some specific build settings need to be set in order to facilitate this:

HEADER_SEARCH_PATHS = $(PROJECT_DIR)/iOS/OpenSSL/include LIBRARY_SEARCH_PATHS = $(inherited) $(PROJECT_DIR)/iOS/OpenSSL/lib/ SWIFT_INCLUDE_PATHS = $(SRCROOT)/iOS/ProjectModules

Receipt validation code is based off this tutorial

IMPORTANT:

In order to fully integrate OpenSSL into our Swift Framework, I had to modify the OpenSSL header files and remove the openssl/ prefix from ALL #import <openssl/{file}.h> references (nb: Jim Rutherford)


Looking for developers for your project?

This project is maintained by Black Pixel. We build apps that companies build their businesses on.

Services: iOS / Android / Web / tvOS / Chromecast / Roku



Website: blackpixel.com  ·  Twitter: @blackpixel  ·  Medium: @bpxl-craft