ios 购买连接器
🛠 为了我们能提供最佳支持,我们恳请您将任何问题提交至 [email protected]
请提交问题时,请指定您的 AppsFlyer 注册(帐户)电子邮件、您的应用程序 ID、生成步骤、日志、代码片段以及任何其他相关信息。
目录
此模块是为以下目的而构建的
- iOS 版本 9 及以上。
- iOS AppsFlyer SDK 6.8.0 及以上版本。
重要提示:Purchase Connector v6.10.1 与 SDK v6.11.0 及以上版本不兼容。如果您正在使用 Purchase Connector v6.10.1,请使用 SDK v6.10.1 或更低的版本。
通过 Cocoapods 将连接器添加到您的项目:
将以下内容添加到 Podfile 中,然后运行 pod install
pod 'PurchaseConnector'
通过 Carthage 将连接器添加到您的项目:
进入仓库根目录下的 Carthage
文件夹。打开 purchase-connector-dynamic.json
或 purchase-connector-static.json
,点击“原始”,复制并将其粘贴到您的 Cartfile
中。
binary "https://raw.githubusercontent.com/AppsFlyerSDK/appsflyer-apple-purchase-connector/main/Carthage/purchase-connector-dynamic.json" == BIINARY_VERSION
binary "https://raw.githubusercontent.com/AppsFlyerSDK/AppsFlyerFramework/master/Carthage/appsflyer-ios.json" ~> 6.10.0
然后在终端中打开项目文件夹,使用命令 carthage update --use-xcframeworks
,然后拖放 PurchaseConnector.xcframework 二进制文件和 AppsFlyerLib.framework(来自 Carthage/Build/iOS 文件夹)。
有关 Carthage 二进制工件集成的更多参考请此处。
通过 SPM 将连接器添加到您的项目:
请按照标准 SPM 依赖关系管理器说明进行,仅在包解析时选择一个包产品。《PurchaseConnector》代表静态链接的二进制文件,《PurchaseConnector-Dynamic》代表动态链接库。
注意:由于 PurchaseConnector 依赖于 AppsFlyerLib 框架,请确保您也将其集成在 Carthage 和 SPM 中。
连接器的基本集成
注意:在实施购买连接器之前,请确保设置 AppsFlyer
appId
和devKey
设置购买连接器
- Swift
// Import the library
import AppsFlyerLib
import StoreKit
import PurchaseConnector
// Default SDK Implementation
AppsFlyerLib.shared().appsFlyerDevKey = "DEV_KEY"
AppsFlyerLib.shared().appleAppID = "APPLE_APP_ID"
//AppsFlyerLib.shared().isDebug = true
// Purchase connector implementation
PurchaseConnector.shared().purchaseRevenueDelegate = self
PurchaseConnector.shared().purchaseRevenueDataSource = self
- Objective-C
// Import the library
#import "AppDelegate.h"
#import <AppsFlyerLib/AppsFLyerLib.h>
#import <PurchaseConnector/PurchaseConnector.h>
// Default SDK implementation
[[AppsFlyerLib shared] setAppleAppID:@"APPLE_APP_ID"];
[[AppsFlyerLib shared] setAppsFlyerDevKey:@"DEV_KEY"];
//[[AppsFlyerLib shared] setIsDebug:YES];
// Purchase Connecor implementation
[[PurchaseConnector shared] setPurchaseRevenueDelegate:self];
[[PurchaseConnector shared] setPurchaseRevenueDataSource:self];
记录自动续订订阅和在应用内购买
启用自动记录应用内购买和自动续订订阅。
- Swift
PurchaseConnector.shared().autoLogPurchaseRevenue = [.autoRenewableSubscriptions, .inAppPurchases]
- Objective-C
[[PurchaseConnector shared] setAutoLogPurchaseRevenue:AFSDKAutoLogPurchaseRevenueOptionsRenewable | AFSDKAutoLogPurchaseRevenueOptionsInAppPurchases];
注意:如果未设置
autoLogPurchaseRevenue
,则默认禁用。该值是一个选项集,因此您可以选择您要观察的用户购买类型。
遵守购买连接器数据源和代理协议
- 为了接收购买验证事件回调,您应该遵守并实现
PurchaseRevenueDelegate
(Swift) 或AppsFlyerPurchaseRevenueDelegate
(Objc-C) 协议。 - 要能够在连接器发送的购买事件中添加您自己的参数,请遵守并实现
PurchaseRevenueDataSource
(Swift) 或AppsFlyerPurchaseRevenueDataSource
(Obj-C) 协议。
- Swift
extension AppDelegate: PurchaseRevenueDataSource, PurchaseRevenueDelegate {
// PurchaseRevenueDelegate method implementation
func didReceivePurchaseRevenueValidationInfo(_ validationInfo: [AnyHashable : Any]?, error: Error?) {
print("PurchaseRevenueDelegate: \(validationInfo)")
print("PurchaseRevenueDelegate: \(error)")
// process validationInfo here
}
// PurchaseRevenueDataSource method implementation
func purchaseRevenueAdditionalParameters(for products: Set<SKProduct>, transactions: Set<SKPaymentTransaction>?) -> [AnyHashable : Any]? {
// Add additional parameters for SKTransactions here.
return ["additionalParameters":["param1":"value1", "param2":"value2"]];
}
}
- Objective-C
@interface AppDelegate () <AppsFlyerPurchaseRevenueDelegate, AppsFlyerPurchaseRevenueDataSource>
@end
@implementation AppDelegate
- (void)applicationDidBecomeActive:(UIApplication *)application {
[[AppsFlyerLib shared] start];
[[PurchaseConnector shared] startObservingTransactions];
}
- (void)didReceivePurchaseRevenueValidationInfo:(NSDictionary *)validationInfo error:(NSError *)error {
NSLog(@"Validation info: %@", validationInfo);
NSLog(@"Error: %@", error);
// Process validation info
}
- (NSDictionary *)purchaseRevenueAdditionalParametersForProducts:(NSSet<SKProduct *> *)products transactions:(NSSet<SKPaymentTransaction *> *)transactions {
return @{@"key1" : @"param1"};
}
@end
开始监控事务
startObservingTransactions
应该被调用来开始监控事务。
注意:应在 AppsFlyer iOS SDK 的 start 方法之后立即调用此方法。
- Swift
PurchaseConnector.shared().startObservingTransactions()
- Objective-C
[[PurchaseConnector shared] startObservingTransactions];
停止监控事务
要停止监控事务,您需要调用 stopObservingTransactions
。
- Swift
PurchaseConnector.shared().stopObservingTransactions()
- Objective-C
[[PurchaseConnector shared] stopObservingTransactions];
注意:如果您调用了
stopObservingTransactions
API,您应该在下次调用startObservingTransactions
之前设置autoLogPurchaseRevenue
值。
在沙盒中测试实现
为了在带有 TestFlight 沙盒帐户的 Xcode 环境中测试真实设备上的购买,您需要将 isSandbox
设置为 true。
- Swift
PurchaseConnector.shared().isSandbox = true
- Objective-C
[[PurchaseConnector shared] setIsSandbox:YES];
*重要提示:在将应用程序发布到生产环境之前,请确保删除
isSandbox
或将其设置为false
。如果在沙盒模式下发送生产购买事件,则您的数据将无法正确验证! *
完整代码示例
Swift 示例
import AppsFlyerLib
import StoreKit
import PurchaseConnector
class AppDelegate: UIResponder, UIApplicationDelegate {
func application(_ _: UIApplication, didFinishLaunchingWithOptions _: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
// Default SDK Implementation
AppsFlyerLib.shared().appsFlyerDevKey = "DEV_KEY"
AppsFlyerLib.shared().appleAppID = "APLE_APP_ID"
// AppsFlyerLib.shared().isDebug = true
// Purchase Connector implementation
PurchaseConnector.shared().purchaseRevenueDelegate = self
PurchaseConnector.shared().purchaseRevenueDataSource = self
PurchaseConnector.shared().autoLogPurchaseRevenue = .autoRenewableSubscriptions
}
func applicationDidBecomeActive(_ application: UIApplication) {
AppsFlyerLib.shared().start()
PurchaseConnector.shared().startObservingTransactions()
}
}
extension AppDelegate: PurchaseRevenueDataSource, PurchaseRevenueDelegate {
// PurchaseRevenueDelegate method implementation
func didReceivePurchaseRevenueValidationInfo(_ validationInfo: [AnyHashable : Any]?, error: Error?) {
print("PurchaseRevenueDelegate: \(validationInfo)")
print("PurchaseRevenueDelegate: \(error)")
// process validationInfo here
}
// PurchaseRevenueDataSource method implementation
func purchaseRevenueAdditionalParameters(for products: Set<SKProduct>, transactions: Set<SKPaymentTransaction>?) -> [AnyHashable : Any]? {
// Add additional parameters for SKTransactions here.
return ["additionalParameters":["param1":"value1", "param2":"value2"]];
}
}
Objective-C 示例
#import "AppDelegate.h"
#import <PurchaseConnector/PurchaseConnector.h>
#import <AppsFlyerLib/AppsFLyerLib.h>
@interface AppDelegate () <AppsFlyerPurchaseRevenueDelegate, AppsFlyerPurchaseRevenueDataSource>
@end
@implementation AppDelegate
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
// Set up AppsFlyerLib first
[[AppsFlyerLib shared] setAppleAppID:@"APPLE_APP_ID"];
[[AppsFlyerLib shared] setAppsFlyerDevKey:@"DEV_KEY"];
// [[AppsFlyerLib shared] setIsDebug:YES];
// Set up PurchaseConnector
[[PurchaseConnector shared] startObservingTransactions];
[[PurchaseConnector shared] setPurchaseRevenueDelegate:self];
[[PurchaseConnector shared] setPurchaseRevenueDataSource:self];
[[PurchaseConnector shared] setAutoLogPurchaseRevenue:AFSDKAutoLogPurchaseRevenueOptionsAutoRenewableSubscriptions];
return YES;
}
- (void)applicationDidBecomeActive:(UIApplication *)application {
[[AppsFlyerLib shared] start];
[[PurchaseConnector shared] startObservingTransactions];
}
- (void)didReceivePurchaseRevenueValidationInfo:(NSDictionary *)validationInfo error:(NSError *)error {
NSLog(@"Validation info: %@", validationInfo);
NSLog(@"Error: %@", error);
// Process validation info
}
- (NSDictionary *)purchaseRevenueAdditionalParametersForProducts:(NSSet<SKProduct *> *)products transactions:(NSSet<SKPaymentTransaction *> *)transactions {
return @{@"key1" : @"param1"};
}
@end