PurchaseStacks
PurchaseStacks 是一个应用内购买的框架。它兼容 Swift 和 Objective-C。API 参考文档可在 ios-sdk.purchasestacks.com 查看
需求
PurchaseFly 至少需要 iOS 12.0 的目标
安装
PurchaseStacks SDK 可以通过 CocoaPods 或手动安装。
通过 CocoaPods 安装
将以下行添加到您的 Podfile 中
pod 'PurchaseStacks', '~> 1.0.0'
然后在终端中运行
pod install
手动安装
将 PurchaseStacks.framework
复制到您的项目中。
初始化
import PurchaseStacks
PSPurchase.config(withAPIKey: "API_KEY", withAPISecret: "API_SECRET")
如果已有用户信息,建议直接在配置中进行跟踪
PSPurchase.config(withAPIKey: "API_KEY", withAPISecret: "API_SECRET", withUserID: "USER_ID", withEmail: "[email protected]")
任何时刻只能实例化一个PSPurchase!使用configure方法让框架为您处理单例实例。
SDK就绪通知
在调用config初始化SDK后,SDK变得就绪时,如果已订阅,它会发送通知
NotificationCenter.default.addObserver(self, selector: #selector(sdkReady(notification:)),
name: Notification.Name("PS_SDK_READY"), object: nil)
@objc func sdkReady(notification: Notification) {
// SDK is ready!
}
跟踪用户信息
PSPurchase.shared.trackUserID("USER_ID", email: "[email protected]", withUsername: "USERNAME")
获取分组
您可以在PurchaseStacks仪表板中组织分组并在此处访问它们。
此功能返回以名称为键的分组字典
PSPurchase.shared.group { (groupsByName, error) in
if let groupsByName = groupsByName {
let group = groupsByName["PREMIUM"]
}
}
此功能返回所有分组数组、活动分组数组和额外信息
PSPurchase.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
}
}
}
恢复购买
PSPurchase.shared.restore { (groupsByName, error) in
if let groupsByName = groupsByName {
// check for the group if it is active and
// provide user with that subscription
}
}
获取消耗性产品
PSPurchase.shared.getAllConsumableProducts { (products, error) in
if let products = products {
// These are only consumable products
}
}
获取关联用户信息
PSPurchase.shared.userInfo { (user, error) in
if let user = user {
// Tracked user
}
}
委派
您可以设置委派,以在任何组发生更改时收到通知。
PSPurchase.shared.delegate = self
当组发生任何更改或订阅发生更改时将调用此方法。
func purchases(_ purchase: PSPurchase, didReceiveUpdatedGroupsInfo groupsByName: [String: PSGroup], withGroups groups: PSGroupsInfo) {
// Check groupByName or groups
}
折扣
有两种折扣可以进行设置和检查:入门级定价和促销定价。您必须首先在App Store连接中配置它们。
检查折扣资格
PSPurchase.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 == PSDiscountType.promo {
PSPurchase.shared.make(with: monthlyProduct, with: promo) { (transaction, groupByName, error, cancelled) in
// check transaction?.group.discount
}
}
}