Skip to content

Instantly share code, notes, and snippets.

@daveweaver
Last active July 3, 2021 15:19
Show Gist options
  • Save daveweaver/707a6bed391d8c00b56d84b491a50863 to your computer and use it in GitHub Desktop.
Save daveweaver/707a6bed391d8c00b56d84b491a50863 to your computer and use it in GitHub Desktop.
Originator / Revenue Cat integration
/*
The main class in this file is the AnalyticsServiceWrapper. It handles any connections to analytics services we
use, such as Google Analytics, crash reporting, revenue reporting, etc., and of course Adjust and Revenue Cat. This
is fairly old code, which is why it's written as a C++ class that contains Objective-C calls.
*/
#import <AdSupport/ASIdentifierManager.h>
#import "Adjust.h"
#import "Purchases.h"
...
static RCPurchases *revCatPurchases;
...
// this is an Objective-C class that serves only to connect Adjust and Rev Cat, as the adjustDelegate.
@interface psdkAdjustToRevCatBridge : NSObject<AdjustDelegate>
@end
@implementation psdkAdjustToRevCatBridge
- (void) adjustAttributionChanged: (nullable ADJAttribution *) attribution {
NSDictionary *attributionData = attribution.dictionary;
if([[ASIdentifierManager sharedManager] isAdvertisingTrackingEnabled]) {
NSUUID *IDFA = [[ASIdentifierManager sharedManager] advertisingIdentifier];
if(IDFA) {
NSMutableDictionary *newData = [NSMutableDictionary dictionaryWithDictionary: attributionData];
newData[@"rc_idfa"] = [IDFA UUIDString];
attributionData = [NSDictionary dictionaryWithDictionary: newData];
}
}
[revCatPurchases addAttributionData: attributionData fromNetwork: RCAttributionNetworkAdjust];
}
@end
// we need to keep our own reference to this because Adjust only weakly retains it.
static psdkAdjustToRevCatBridge *adjustToRevCatBridgeInstance = nil;
@interface psdkDummyRevCatDelegate : NSObject<RCPurchasesDelegate>
@end
@implementation psdkDummyRevCatDelegate
- (void) purchases: (RCPurchases *) purchases completedTransaction: (SKPaymentTransaction *) transaction withUpdatedInfo: (RCPurchaserInfo *) purchaserInfo { }
- (void) purchases: (RCPurchases *) purchases failedTransaction: (SKPaymentTransaction *) transaction withReason: (NSError *) failureReason { }
- (void) purchases: (RCPurchases *) purchases receivedUpdatedPurchaserInfo: (RCPurchaserInfo *) purchaserInfo {
// all_access is what the subscription entitlements are named in the Rev Cat portal. If we have a new app that isn't named this, we'll need to parametrize this.
if(![purchaserInfo.activeEntitlements containsObject: @"all_access"] && [[psdkMonetizer sharedInstance] isSubscribedToAnySubscription])
[RCPurchases.sharedPurchases restoreTransactionsWithCompletionBlock: ^(RCPurchaserInfo * _Nullable, NSError * _Nullable) {
// nothing here for now
}];
}
- (void) purchases: (RCPurchases *) purchases restoredTransactionsWithPurchaserInfo: (RCPurchaserInfo *) purchaserInfo { }
- (void) purchases: (RCPurchases *) purchases failedToRestoreTransactionsWithError: (NSError *) error { }
- (void) purchases: (RCPurchases *) purchases failedToUpdatePurchaserInfoWithError: (NSError *) error { }
@end
static psdkDummyRevCatDelegate *dummyRevCatDelegateInstance = nil;
...
// this gets called on app launch, from application:didFinishLaunchingWithOptions.
void AnalyticsServiceWrapper::initialize(string adjustAppToken, string revenueCatAPIKey, bool isDebugMode) {
...
NSString *adjustEnvironment = (isDebugMode ? ADJEnvironmentSandbox : ADJEnvironmentProduction);
ADJConfig *adjustConfig = [ADJConfig configWithAppToken: trans::stringToObC((adjustAppToken))
environment: adjustEnvironment
allowSuppressLogLevel: YES];
[adjustConfig setLogLevel: ADJLogLevelSuppress];
[RCPurchases configureWithAPIKey: trans::stringToObC(revenueCatAPIKey)];
RCPurchases.sharedPurchases.finishTransactions = NO;
dummyRevCatDelegateInstance = [[psdkDummyRevCatDelegate alloc] init];
RCPurchases.sharedPurchases.delegate = dummyRevCatDelegateInstance;
adjustToRevCatBridgeInstance = [[psdkAdjustToRevCatBridge alloc] init];
adjustConfig.delegate = adjustToRevCatBridgeInstance;
[Adjust appDidLaunch:adjustConfig];
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment