CocoaPods trunk is moving to be read-only. Read more on the blog, there are 18 months to go.

PurchaseFly 1.0.23

PurchaseFly 1.0.23

Maintained by Rifat Monzur.



  • By
  • Purchase Stacks

PurchaseFly

PurchaseFly is a framework for in app purchase. It works with both Swift and Objective-C. API reference is available at ios-sdk.purchasefly.com

PurchaseFly

Requirements

PurchaseFly requires minimum targets iOS 12.0

Installation

PurchaseFly SDK can be installed via CocoaPods or manually.

Install via CocoaPods

Add the following line to your Podfile:

pod 'PurchaseFly', '~> 1.0.23'

And then run in the Terminal:

pod install

Manual Installation

Copy PurchaseFly.framework to your project.

Intialization

import PurchaseFly

PFPurchase.config(withAPIKey: "API_KEY", withAPISecret: "API_SECRET")

If you have user info, it's recommended to track directly in config

PFPurchase.config(withAPIKey: "API_KEY", withAPISecret: "API_SECRET", withUserID: "USER_ID")

Only one instance of PFPurchase should be instantiated at a time! Use the configure method to let the framework handle the singleton instance for you.

SDK Ready Notification

After config is called to initialize the SDK, when SDK becomes ready it sends notification, if it is subscribed to

NotificationCenter.default.addObserver(self, selector: #selector(sdkReady(notification:)),
                name: Notification.Name("PF_SDK_READY"), object: nil)
@objc func sdkReady(notification: Notification) {
    // SDK is ready!
}

Track user info

PFPurchase.shared.trackUser("USER_ID")

To enable purchase sharing for the tracked user across devices or operating systems, you need to put enable purchase sharing for the user. And put it before trackUser call. It is false by default.

PFPurchase.shared.allowPurchaseSharing = true
PFPurchase.shared.trackUser("USER_ID")

Get Groups

You can organize the groups in PurchaseFly Dashboard and access them here.

This returns a dictionary of groups with names as keys

PFPurchase.shared.group { (groupsByName, error) in
    if let groupsByName = groupsByName {
        let group = groupsByName["PREMIUM"]
    }
}

This returns all groups array, active groups array and additonal information

PFPurchase.shared.groups { (groupsInfo, error) in
    // groupsInfo contains all groups information
}

Get Package & Product

if let group = group {
    let monthlyPackage = group.packageByName["Monthly"]
    if let monthlyPackage = monthlyPackage {
        let monthlyProduct = monthlyPackage.activeProduct()
    }

    let yearlyPackage = group.packageByName["Yearly"]
    if let yearlyPackage = yearlyPackage {
        let yearlyProduct = yearlyPackage.activeProduct()
    }
}

Make Purchase

Purchase with product

PFPurchase.shared.make(with: monthlyProduct) { (transaction, groupsByName, error, cancelled) in
    if let groupsByName = groupsByName {
        let group = groupsByName["PREMIUM"]
        if (group.isActive) {
            // Purchase is successful
        }
    }
}

Purchase with package

PFPurchase.shared.make(with: monthlyPackage) { (transaction, groupsByName, error, cancelled) in
    if let groupsByName = groupsByName {
        let group = groupsByName["PREMIUM"]
        if (group.isActive) {
            // Purchase is successful
        }
    }
}

Restore purchase

PFPurchase.shared.restore { (groupsByName, error) in
    if let groupsByName = groupsByName {
        // check for the group if it is active and
        // provide user with that subscription
    }
}

Get Consumable products

PFPurchase.shared.getAllConsumableProducts { (products, error) in
    if let products = products {
        // These are only consumable products
    }
}

Get associated user info

PFPurchase.shared.userInfo { (user, error) in
    if let user = user {
        // Tracked user
    }
}

Delegate

You can setup delegate to be notified whenver any changes to the groups happen

PFPurchase.shared.delegate = self

This method will be called when any change to groups happen or any change to subscriptions take place

func purchases(_ purchase: PFPurchase, didReceiveUpdatedGroupsInfo groupsByName: [String: PFGroup], withGroups groups: PFGroupsInfo) {
    // Check groupByName or groups
}

Discount

There's two kinds of discounts that can be set and check against. Introductory pricing and promotional pricing. You have to configure them first at appstore connect.

Check for discount eligibility

PFPurchase.shared.checkDiscountEligibility(monthlyProduct.productId, with: .promo) { (status, error) in
    // Check if user is eligible for promo type discount
}

Apply Promo discount and make purchase

let discounts = monthlyProduct.productDiscounts()
if let promo = discounts.first {
    if promo.discountType == PFDiscountType.promo {
        PFPurchase.shared.make(with: monthlyProduct, with: promo) { (transaction, groupByName, error, cancelled) in
            // check transaction?.group.discount
        }
    }
}