PurchaseFly
PurchaseFly 是一个内购框架。它支持 Swift 和 Objective-C。API 参考可在 ios-sdk.purchasefly.com 找到
要求
PurchaseFly 至少需要目标 iOS 12.0
安装
PurchaseFly SDK 可通过 CocoaPods 或手动安装。
通过 CocoaPods 安装
将以下行添加到您的 Podfile 中
pod 'PurchaseFly', '~> 1.0.23'
然后在终端中运行
pod install
手动安装
将 PurchaseFly.framework
复制到您的项目中。
初始化
import PurchaseFly
PFPurchase.config(withAPIKey: "API_KEY", withAPISecret: "API_SECRET")
如果有用户信息,建议直接在配置中跟踪。
PFPurchase.config(withAPIKey: "API_KEY", withAPISecret: "API_SECRET", withUserID: "USER_ID")
一次只能实例化一个PFPurchase!使用配置方法让框架为您处理单例实例。
SDK就绪通知
调用config
初始化SDK后,当SDK就绪时,如果已订阅,则会发送通知。
NotificationCenter.default.addObserver(self, selector: #selector(sdkReady(notification:)),
name: Notification.Name("PF_SDK_READY"), object: nil)
@objc func sdkReady(notification: Notification) {
// SDK is ready!
}
跟踪用户信息
PFPurchase.shared.trackUser("USER_ID")
要实现跨设备或操作系统跟踪用户的购买共享,您需要为用户启用购买共享。并将其放置在调用trackUser
之前。默认值为false
。
PFPurchase.shared.allowPurchaseSharing = true
PFPurchase.shared.trackUser("USER_ID")
获取分组
您可以在PurchaseFly仪表板中组织分组,并在这里访问它们。
这将返回以名称为键的分组字典。
PFPurchase.shared.group { (groupsByName, error) in
if let groupsByName = groupsByName {
let group = groupsByName["PREMIUM"]
}
}
这将返回所有分组数组、活动分组数组和附加信息。
PFPurchase.shared.groups { (groupsInfo, error) in
// groupsInfo contains all groups information
}
获取套餐 & 产品
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()
}
}
进行购买
购买产品
PFPurchase.shared.make(with: monthlyProduct) { (transaction, groupsByName, error, cancelled) in
if let groupsByName = groupsByName {
let group = groupsByName["PREMIUM"]
if (group.isActive) {
// Purchase is successful
}
}
}
购买套餐
PFPurchase.shared.make(with: monthlyPackage) { (transaction, groupsByName, error, cancelled) in
if let groupsByName = groupsByName {
let group = groupsByName["PREMIUM"]
if (group.isActive) {
// Purchase is successful
}
}
}
恢复购买
PFPurchase.shared.restore { (groupsByName, error) in
if let groupsByName = groupsByName {
// check for the group if it is active and
// provide user with that subscription
}
}
获取消耗品产品
PFPurchase.shared.getAllConsumableProducts { (products, error) in
if let products = products {
// These are only consumable products
}
}
获取关联用户信息
PFPurchase.shared.userInfo { (user, error) in
if let user = user {
// Tracked user
}
}
委托
您可以设置委托,以便在组中发生任何变化时进行通知
PFPurchase.shared.delegate = self
当组或订阅发生变化时,将调用此方法
func purchases(_ purchase: PFPurchase, didReceiveUpdatedGroupsInfo groupsByName: [String: PFGroup], withGroups groups: PFGroupsInfo) {
// Check groupByName or groups
}
折扣
可以设置和检查两种折扣,分别是入门级定价和促销定价。您必须在App Store Connect中进行配置。
检查折扣资格
PFPurchase.shared.checkDiscountEligibility(monthlyProduct.productId, with: .promo) { (status, error) in
// Check if user is eligible for promo type discount
}
应用促销折扣并购买
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
}
}
}