https://github.com/appsflyersdk/appsflyer-apple-purchase-connector
https://github.com/appsflyersdk/appsflyer-apple-purchase-connector
Last synced: 6 months ago
JSON representation
- Host: GitHub
- URL: https://github.com/appsflyersdk/appsflyer-apple-purchase-connector
- Owner: AppsFlyerSDK
- License: other
- Created: 2021-10-01T10:06:56.000Z (over 3 years ago)
- Default Branch: main
- Last Pushed: 2024-08-29T11:07:09.000Z (9 months ago)
- Last Synced: 2024-10-31T15:34:58.796Z (7 months ago)
- Language: Objective-C
- Size: 18.4 MB
- Stars: 3
- Watchers: 6
- Forks: 3
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
![]()
# iOS Purchase Connector
[](https://github.com/AppsFlyerSDK/android-purchase-connector/blob/main/LICENSE)
🛠In order for us to provide optimal support, we would kindly ask you to submit any issues to
[email protected]> *When submitting an issue please specify your AppsFlyer sign-up (account) email , your app ID , production steps, logs, code snippets and any additional relevant information.
## Table Of Content
* [This Module is Built for](#plugin-build-for)
* [Adding The Connector To Your Project](#install-connector)
+ [Cocoapods](#cocoapods)
+ [Carthage](#carthage)
+ [SPM](#spm)
* [Basic Integration Of The Connector](#basic-integration)
+ [Set up Purchase Connector](#create-instance)
+ [Log Auto-Renewable Subscriptions and In-App Purchases](#log-subscriptions)
+ [Conform to Purchase Connector Data Source and Delegate protocols](#conforming)
+ [Start Observing Transactions](#start)
+ [Stop Observing Transactions](#stop)
* [Testing the implementation in Sandbox](#testing)
* [Full Code Examples](#example)> *IMPORTNANT NOTE: Please, be sure to check Purchase Connector and AppsFlyerFramework version compatability table and use corresponding versions to avoid unexpected behaviour *
| PurchaseConnector | AppsFlyerSDK |
| :------: | :--------: |
| 6.8.0 | 6.8.0 - 6.9.2 |
| 6.8.1 | 6.8.0 - 6.9.2 |
| 6.10.0 | 6.10.0 |
| 6.10.1 | 6.10.1 |
| 6.12.2 | 6.12.2 |
| 6.12.3 | 6.12.2 |// for dynamically linked dependency
pod 'PurchaseConnector/Dynamic'// for statically linked Strict dependency (disabled IDFA collection)
pod 'PurchaseConnector/Strict'
```More reference on Carthage binary artifacts integration [here](https://github.com/Carthage/Carthage/blob/master/Documentation/Artifacts.md).
> *Note: This repository contains statically linked `PurchaseConnector.xcframework`. If you want to use dynamic .xcframework, please integrate it for SPM from this repository:
https://github.com/AppsFlyerSDK/PurchaseConnector-Dynamic*> *Note: as PurchaseConnector has a dependency on [AppsFlyerLib framework](https://github.com/AppsFlyerSDK/AppsFlyerFramework), please, make sure to integrate it as well for Carthage and SPM.*
// 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
```objective-c
// Import the library
#import "AppDelegate.h"
#import
#import// 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];
```### Log Auto-Renewable Subscriptions and In-App Purchases
Enables automatic logging of In-App purchases and Auto-renewable subscriptions.
- Swift
```swift
PurchaseConnector.shared().autoLogPurchaseRevenue = [.autoRenewableSubscriptions, .inAppPurchases]
```- Objective-C
```objective-c
[[PurchaseConnector shared] setAutoLogPurchaseRevenue:AFSDKAutoLogPurchaseRevenueOptionsRenewable | AFSDKAutoLogPurchaseRevenueOptionsInAppPurchases];
```> *Note: if `autoLogPurchaseRevenue` has not been set, it is disabled by default. The value is an option set, so you can choose what kind of user purchases you want to observe.*
### Conform to Purchase Connector Data Source and Delegate protocols
* In order to receive purchase validation event callbacks, you should conform to and implement `PurchaseRevenueDelegate`(Swift) or `AppsFlyerPurchaseRevenueDelegate`(Objc-C) protocol.
* To be able to add your custom parameters to the purchase event, that Connector sends, please conform to and implement `PurchaseRevenueDataSource`(Swift) or `AppsFlyerPurchaseRevenueDataSource`(Obj-C) protocol.- Swift
```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, transactions: Set?) -> [AnyHashable : Any]? {
// Add additional parameters for SKTransactions here.
return ["additionalParameters":["param1":"value1", "param2":"value2"]];
}
}
```- Objective-C
```objective-c
@interface AppDelegate ()
@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 *)products transactions:(NSSet *)transactions {
return @{@"key1" : @"param1"};
}@end
```### Start Observing Transactions
`startObservingTransactions` should be called to start observing transactions.
> *Note: This should be called right after the AppsFlyer iOS SDK's start method..*- Swift
```swift
PurchaseConnector.shared().startObservingTransactions()
```- Objective-C
```objective-c
[[PurchaseConnector shared] startObservingTransactions];
```### Stop Observing Transactions
To stop observing transactions, you need to call `stopObservingTransactions`.
- Swift
```swift
PurchaseConnector.shared().stopObservingTransactions()
```- Objective-C
```objective-c
[[PurchaseConnector shared] stopObservingTransactions];
```> *Note: if you called `stopObservingTransactions` API, you should set `autoLogPurchaseRevenue` value before you call `startObservingTransactions` next time.*
## Testing the implementation in Sandbox
In order to test purchases in Xcode environment on a real device with TestFlight sandbox account, you need to set `isSandbox` to true.
- Swift
```swift
PurchaseConnector.shared().isSandbox = true
```- Objective-C
```objective-c
[[PurchaseConnector shared] setIsSandbox:YES];
```> *IMPORTANT NOTE: Before releasing your app to production please be sure to remove `isSandbox` or set it to `false`. If the production purchase event will be sent in sandbox mode, your event will not be validated properly! *
***
### Swift Example
```swift
import AppsFlyerLib
import StoreKit
import PurchaseConnectorclass 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, transactions: Set?) -> [AnyHashable : Any]? {
// Add additional parameters for SKTransactions here.
return ["additionalParameters":["param1":"value1", "param2":"value2"]];
}
}
```### Objective-C Example
```objective-c
#import "AppDelegate.h"
#import
#import@interface AppDelegate ()
@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] 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 *)products transactions:(NSSet *)transactions {
return @{@"key1" : @"param1"};
}@end
```