需求
ImplusPurcases SDK 最低要求
- iOS 9.0+
- Xcode 10+
安装
CocoaPods
使用 CocoaPods 将 ImplusPurchases
集成到您的 Xcode 项目中,请在 Podfile
中指定。
target 'MyApp' do
use_frameworks!
pod 'ImplusPurchases', '~> 0.1'
end
导入 ImplusPurchases
import ImplusPurchases
设置
基本配置
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
IMPurchase.shared.enableLog(true)
SDK: There are two initialization methods, please choose one of them to initialize
//Step 1: Initialize using the specified'appkey'
IMPurchase.shared.configure("appkey")
//Step 2: Import the'appflow-app-token.plist' file and use the following method to initialize
//IMPurchase.shared.configureInitSDKWithAppFlowPlist
}
使用应用密钥和用户 ID
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
IMPurchase.shared.enableLog(true)
IMPurchase.shared.configure("appkey", userId: "userId")
}
获取产品组
要获取产品组,您需要调用 getProductGroups
函数。它将返回一个以组名为键、值为 IMGroup
的字典。
IMPurchase.shared.getProductGroups { (productGroups, error) in
if let group = productGroups["pro"] {
let products = group.products
}
}
要获取IMProduct
数,您需要检查您想要的产品组是否存在于产品组字典中。然后,您可以调用group.products
来获取产品数组。
检查支付是否可用
在开始购买过程之前,您需要检查设备上是否可以购买。这可能由于家长控制或其他原因而被禁用。
let canMakePurchases = IMPurchase.shared.canMakePayments()
进行购买
要开始购买过程,请调用函数purchaseProduct
,它将以IMProduct
对象作为参数。
IMPurchase.shared.purchaseProduct(selectedPlan) { (transaction, entitlement, error, isCanceled) in {
}
闭包将返回SKPaymentTransaction
,权限字典,错误(如果发生)以及布尔值,表示用户是否已取消购买过程。
检查用户状态
要检查用户是否有任何活动的权限,请调用函数hasActiveSubscription
。
IMPurchase.shared.hasActiveSubscription { (entitlements, error) in
if entitlements["pro"]?.isActive() == true {
// user have active entiltement
} else {
// user don't have active entitlement
}
}
它将返回权限字典,其中键为权限ID,值为IMEntitlment
对象。要检查权限是否激活,请调用IMEntitlement
对象的isActive()
方法。
注意
hasActiveSubscription
将返回缓存的结果。此函数偶尔会进行网络调用以刷新缓存信息。要清除缓存,请调用IMPurchase.shared.invalidateCache()
。
恢复购买
要恢复用户的购买,请调用restorePurchases
函数。
IMPurchase.shared.restorePurchases { (entitlement, error) in
if entitlement["pro"]?.isActive() == true {
// Purchase restored and have active entitlment
} else {
if error != nil {
// Purchase restore finished with error
} else {
// Purchase restore finished but user don't have active entitlment
}
}
UploadUserInfo
要上传用户信息,请调用 uploadUserInfo
函数。此方法需要传入参数 userId
。
注意 添加了界面
uploadUserInfo
,该界面用于上传已订阅的用户数据。目前,这些数据用于 IAP 推广归因。
swift
IMPurchase.shared.uploadUserInfo(userId: "xxxx") { (result, error) in
}
Objective-C
[[IMPurchase shared] uploadUserInfoWithUserId:@"xxxx" completion:^(BOOL result, NSError * _Nullable error) {
}];
出现错误(如果发生)和一个布尔值,指示提交数据的成功或失败