• Stars
    star
    500
  • Rank 88,178 (Top 2 %)
  • Language
    C
  • License
    MIT License
  • Created over 6 years ago
  • Updated over 4 years ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

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

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

More Repositories

1

BPXLUUIDHandler

Objective-C
214
star
2

react-component-hierarchy

A CLI app for visualizing how React components are structured in a project.
JavaScript
119
star
3

StatusBarLayer

A Framer module for generating accurate, customizable status bars for iOS app prototypes.
CoffeeScript
52
star
4

ControlPanelLayer

A Framer module for creating a developer panel to control aspects of the prototype from within the prototype.
CoffeeScript
34
star
5

RemoteLayer

A Framer module to generate an interactive Apple TV remote for tvOS app prototypes.
CoffeeScript
30
star
6

redux-generators

CLI tool for quickly and easily scaffolding Redux applications
JavaScript
29
star
7

FocusEngine

A Framer module for simulating the grid focus behavior found on Apple TV and Roku.
CoffeeScript
23
star
8

GradientHelper

A Framer module for applying gradients to layers and animating them.
CoffeeScript
21
star
9

MapboxLayer

A Framer module for creating powerful Mapbox maps in your prototypes.
CoffeeScript
20
star
10

CarouselComponent

A Framer module for creating a scrolling carousel of items in various configurations.
CoffeeScript
18
star
11

NavbarComponent

A Framer module for generating an iOS-style navigation bar.
CoffeeScript
16
star
12

redux-handbook

Black Pixel Redux Handbook
CSS
12
star
13

AutoDC

Automated data-centric processing
Python
10
star
14

BreakOut

BreakOut Demo for the iPad featured at the Seattle Voices That Matter Conference
C++
7
star
15

firefly

Shell
7
star
16

terraform-aws-s3-static-site

HCL
6
star
17

terraform-kubernetes-flux2-bootstrap

Boostrap a flux deployment on EKS
HCL
5
star
18

usoniancss

A CSS toolkit for building scalable, performant UI.
CSS
4
star
19

hue-gallery

A client-side web app that extracts an imageโ€™s color palette and sends it to the Philips Hue API. Featured in Net Magazine's December 2016 issue.
JavaScript
4
star
20

framer-module-boilerplate

Framer Module Boilerplate
CoffeeScript
3
star
21

payload-command-console

C++
3
star
22

hyperdrive

Python
2
star
23

redux-todomvc

Black Pixel's Redux Todomvc example. Architecture based on http://bpxl-labs.github.io/redux-handbook/
JavaScript
2
star
24

DebugInjectorSample

Securely add debug-only functionality to Android apps and test locale changes in debug builds.
Java
1
star
25

metal-lab

Getting the basic pipeline of metal figured out
Swift
1
star
26

bpxl-animation-samples

Examples illustrating iOS 10โ€™s UIPropertyViewAnimator API.
Swift
1
star