fix:1、接入外部sdk
This commit is contained in:
@@ -6,7 +6,22 @@
|
||||
//
|
||||
|
||||
#import "AppsFlyeriOSWrapper.h"
|
||||
#import <objc/runtime.h>
|
||||
|
||||
#import <StoreKit/StoreKit.h>
|
||||
#import "UnityFramework/UnityFramework-Swift.h"
|
||||
|
||||
#if __has_include(<PurchaseConnector/PurchaseConnector-Swift.h>)
|
||||
#import <PurchaseConnector/PurchaseConnector-Swift.h>
|
||||
#elif __has_include("PurchaseConnector-Swift.h")
|
||||
#import "PurchaseConnector-Swift.h"
|
||||
#endif
|
||||
|
||||
#if __has_include(<UnityFramework/UnityFramework-Swift.h>)
|
||||
#import <UnityFramework/UnityFramework-Swift.h>
|
||||
#elif __has_include("UnityFramework-Swift.h")
|
||||
#import "UnityFramework-Swift.h"
|
||||
#endif
|
||||
|
||||
static void unityCallBack(NSString* objectName, const char* method, const char* msg) {
|
||||
if(objectName){
|
||||
@@ -18,7 +33,7 @@ extern "C" {
|
||||
|
||||
const void _startSDK(bool shouldCallback, const char* objectName) {
|
||||
[[AppsFlyerLib shared] setPluginInfoWith: AFSDKPluginUnity
|
||||
pluginVersion:@"6.16.21"
|
||||
pluginVersion:@"6.17.91"
|
||||
additionalParams:nil];
|
||||
startRequestObjectName = stringFromChar(objectName);
|
||||
AppsFlyeriOSWarpper.didCallStart = YES;
|
||||
@@ -273,21 +288,19 @@ extern "C" {
|
||||
}];
|
||||
}
|
||||
|
||||
const void _validateAndSendInAppPurchaseV2 (const char* product, const char* price, const char* currency, const char* transactionId, const char* extraEventValues, const char* objectName) {
|
||||
const void _validateAndSendInAppPurchaseV2 (const char* product, const char* transactionId, int purchaseType, const char* purchaseAdditionalDetails, const char* objectName) {
|
||||
|
||||
validateAndLogObjectName = stringFromChar(objectName);
|
||||
AFSDKPurchaseDetails *details = [[AFSDKPurchaseDetails alloc] initWithProductId:stringFromChar(product) price:stringFromChar(price) currency:stringFromChar(currency) transactionId:stringFromChar(transactionId)];
|
||||
AFSDKPurchaseDetails *details = [[AFSDKPurchaseDetails alloc] initWithProductId:stringFromChar(product) transactionId:stringFromChar(transactionId) purchaseType:(AFSDKPurchaseType)purchaseType];
|
||||
|
||||
[[AppsFlyerLib shared]
|
||||
validateAndLogInAppPurchase:details
|
||||
extraEventValues:dictionaryFromJson(extraEventValues)
|
||||
completionHandler:^(AFSDKValidateAndLogResult * _Nullable result) {
|
||||
if (result.status == AFSDKValidateAndLogStatusSuccess) {
|
||||
unityCallBack(validateAndLogObjectName, VALIDATE_AND_LOG_V2_CALLBACK, stringFromdictionary(result.result));
|
||||
} else if (result.status == AFSDKValidateAndLogStatusFailure) {
|
||||
unityCallBack(validateAndLogObjectName, VALIDATE_AND_LOG_V2_CALLBACK, stringFromdictionary(result.errorData));
|
||||
purchaseAdditionalDetails:dictionaryFromJson(purchaseAdditionalDetails)
|
||||
completion:^(NSDictionary * _Nullable response, NSError * _Nullable error) {
|
||||
if (error) {
|
||||
unityCallBack(validateAndLogObjectName, VALIDATE_AND_LOG_V2_ERROR_CALLBACK, stringFromdictionary(dictionaryFromNSError(error)));
|
||||
} else {
|
||||
unityCallBack(validateAndLogObjectName, VALIDATE_AND_LOG_V2_ERROR_CALLBACK, stringFromdictionary(dictionaryFromNSError(result.error)));
|
||||
unityCallBack(validateAndLogObjectName, VALIDATE_AND_LOG_V2_CALLBACK, stringFromdictionary(response));
|
||||
}
|
||||
}];
|
||||
|
||||
@@ -337,6 +350,97 @@ extern "C" {
|
||||
[AppsFlyerLib shared].disableIDFVCollection = isDisabled;
|
||||
}
|
||||
|
||||
// Purchase connector
|
||||
const void _startObservingTransactions() {
|
||||
[[PurchaseConnector shared] startObservingTransactions];
|
||||
}
|
||||
|
||||
const void _stopObservingTransactions() {
|
||||
[[PurchaseConnector shared] stopObservingTransactions];
|
||||
}
|
||||
|
||||
const void _setIsSandbox(bool isSandBox) {
|
||||
[[PurchaseConnector shared] setIsSandbox:isSandBox];
|
||||
}
|
||||
|
||||
const void _setPurchaseRevenueDelegate() {
|
||||
if (_AppsFlyerdelegate== nil) {
|
||||
_AppsFlyerdelegate = [[AppsFlyeriOSWarpper alloc] init];
|
||||
}
|
||||
[[PurchaseConnector shared] setPurchaseRevenueDelegate:_AppsFlyerdelegate];
|
||||
}
|
||||
|
||||
const void _setAutoLogPurchaseRevenue(int option) {
|
||||
[[PurchaseConnector shared] setAutoLogPurchaseRevenue:option];
|
||||
|
||||
}
|
||||
|
||||
const void _initPurchaseConnector(const char* objectName) {
|
||||
if (_AppsFlyerdelegate == nil) {
|
||||
_AppsFlyerdelegate = [[AppsFlyeriOSWarpper alloc] init];
|
||||
}
|
||||
onPurchaseValidationObjectName = stringFromChar(objectName);
|
||||
}
|
||||
|
||||
const void _setPurchaseRevenueDataSource(const char* objectName) {
|
||||
if (_AppsFlyerdelegate == nil) {
|
||||
_AppsFlyerdelegate = [[AppsFlyeriOSWarpper alloc] init];
|
||||
}
|
||||
|
||||
if (strstr(objectName, "StoreKit2") != NULL) {
|
||||
|
||||
// Force protocol conformance
|
||||
Protocol *sk2Protocol = @protocol(AppsFlyerPurchaseRevenueDataSourceStoreKit2);
|
||||
class_addProtocol([_AppsFlyerdelegate class], sk2Protocol);
|
||||
|
||||
if (![_AppsFlyerdelegate conformsToProtocol:@protocol(AppsFlyerPurchaseRevenueDataSourceStoreKit2)]) {
|
||||
NSLog(@"[AppsFlyer] Warning: SK2 protocol not conformed!");
|
||||
}
|
||||
}
|
||||
|
||||
[PurchaseConnector shared].purchaseRevenueDataSource = _AppsFlyerdelegate;
|
||||
}
|
||||
|
||||
const void _setStoreKitVersion(int storeKitVersion) {
|
||||
[[PurchaseConnector shared] setStoreKitVersion:(AFSDKStoreKitVersion)storeKitVersion];
|
||||
}
|
||||
|
||||
const void _logConsumableTransaction(const char* transactionId) {
|
||||
if (@available(iOS 15.0, *)) {
|
||||
NSString *transactionIdStr = [NSString stringWithUTF8String:transactionId];
|
||||
[AFUnityStoreKit2Bridge fetchAFSDKTransactionSK2WithTransactionId:transactionIdStr completion:^(AFSDKTransactionSK2 *afTransaction) {
|
||||
if (afTransaction) {
|
||||
[[PurchaseConnector shared] logConsumableTransaction:afTransaction];
|
||||
} else {
|
||||
NSLog(@"No AFSDKTransactionSK2 found for id %@", transactionIdStr);
|
||||
}
|
||||
}];
|
||||
}
|
||||
}
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
typedef const char *(*UnityPurchaseCallback)(const char *, const char *);
|
||||
|
||||
UnityPurchaseCallback UnityPurchasesGetAdditionalParamsCallback = NULL;
|
||||
UnityPurchaseCallback UnityPurchasesGetAdditionalParamsCallbackSK2 = NULL;
|
||||
|
||||
__attribute__((visibility("default")))
|
||||
void RegisterUnityPurchaseRevenueParamsCallback(UnityPurchaseCallback callback) {
|
||||
UnityPurchasesGetAdditionalParamsCallback = callback;
|
||||
}
|
||||
|
||||
__attribute__((visibility("default")))
|
||||
void RegisterUnityPurchaseRevenueParamsCallbackSK2(UnityPurchaseCallback callback) {
|
||||
UnityPurchasesGetAdditionalParamsCallbackSK2 = callback;
|
||||
}
|
||||
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
}
|
||||
|
||||
@implementation AppsFlyeriOSWarpper
|
||||
@@ -378,5 +482,120 @@ static BOOL didCallStart;
|
||||
unityCallBack(onDeeplinkingObjectName, ON_DEEPLINKING, stringFromdictionary(dict));
|
||||
}
|
||||
|
||||
// Purchase Connector
|
||||
- (void)didReceivePurchaseRevenueValidationInfo:(NSDictionary *)validationInfo error:(NSError *)error {
|
||||
if (error != nil) {
|
||||
unityCallBack(onPurchaseValidationObjectName, PURCHASE_REVENUE_ERROR_CALLBACK, [[error localizedDescription] UTF8String]);
|
||||
} else {
|
||||
unityCallBack(onPurchaseValidationObjectName, PURCHASE_REVENUE_VALIDATION_CALLBACK, stringFromdictionary(validationInfo));
|
||||
}
|
||||
}
|
||||
|
||||
- (NSDictionary *)purchaseRevenueAdditionalParametersForProducts:(NSSet<SKProduct *> *)products
|
||||
transactions:(NSSet<SKPaymentTransaction *> *)transactions {
|
||||
|
||||
NSMutableArray *productsArray = [NSMutableArray array];
|
||||
for (SKProduct *product in products) {
|
||||
[productsArray addObject:@{
|
||||
@"productIdentifier": product.productIdentifier ?: @"",
|
||||
@"localizedTitle": product.localizedTitle ?: @"",
|
||||
@"localizedDescription": product.localizedDescription ?: @"",
|
||||
@"price": [product.price stringValue] ?: @""
|
||||
}];
|
||||
}
|
||||
|
||||
NSMutableArray *transactionsArray = [NSMutableArray array];
|
||||
for (SKPaymentTransaction *txn in transactions) {
|
||||
[transactionsArray addObject:@{
|
||||
@"transactionIdentifier": txn.transactionIdentifier ?: @"",
|
||||
@"transactionState": @(txn.transactionState),
|
||||
@"transactionDate": txn.transactionDate ? [@(txn.transactionDate.timeIntervalSince1970) stringValue] : @""
|
||||
}];
|
||||
}
|
||||
|
||||
NSDictionary *input = @{
|
||||
@"products": productsArray,
|
||||
@"transactions": transactionsArray
|
||||
};
|
||||
|
||||
NSError *error = nil;
|
||||
NSData *jsonData = [NSJSONSerialization dataWithJSONObject:input options:0 error:&error];
|
||||
if (error || !jsonData) {
|
||||
NSLog(@"[AppsFlyer] Failed to serialize Unity purchase data: %@", error);
|
||||
return @{};
|
||||
}
|
||||
|
||||
NSString *jsonString = [[NSString alloc] initWithData:jsonData encoding:NSUTF8StringEncoding];
|
||||
if (!jsonString || !UnityPurchasesGetAdditionalParamsCallback) {
|
||||
NSLog(@"[AppsFlyer] Unity callback not registered");
|
||||
return @{};
|
||||
}
|
||||
|
||||
const char *resultCStr = UnityPurchasesGetAdditionalParamsCallback([jsonString UTF8String], "");
|
||||
if (!resultCStr) {
|
||||
NSLog(@"[AppsFlyer] Unity callback returned null");
|
||||
return @{};
|
||||
}
|
||||
|
||||
NSString *resultJson = [NSString stringWithUTF8String:resultCStr];
|
||||
NSData *resultData = [resultJson dataUsingEncoding:NSUTF8StringEncoding];
|
||||
NSDictionary *parsedResult = [NSJSONSerialization JSONObjectWithData:resultData options:0 error:&error];
|
||||
|
||||
if (error || ![parsedResult isKindOfClass:[NSDictionary class]]) {
|
||||
NSLog(@"[AppsFlyer] Failed to parse Unity response: %@", error);
|
||||
return @{};
|
||||
}
|
||||
|
||||
return parsedResult;
|
||||
}
|
||||
|
||||
#pragma mark - AppsFlyerPurchaseRevenueDataSourceStoreKit2
|
||||
- (NSDictionary *)purchaseRevenueAdditionalParametersStoreKit2ForProducts:(NSSet<AFSDKProductSK2 *> *)products transactions:(NSSet<AFSDKTransactionSK2 *> *)transactions {
|
||||
if (@available(iOS 15.0, *)) {
|
||||
NSArray *productInfoArray = [AFUnityStoreKit2Bridge extractSK2ProductInfo:[products allObjects]];
|
||||
NSArray *transactionInfoArray = [AFUnityStoreKit2Bridge extractSK2TransactionInfo:[transactions allObjects]];
|
||||
|
||||
NSDictionary *input = @{
|
||||
@"products": productInfoArray,
|
||||
@"transactions": transactionInfoArray
|
||||
};
|
||||
|
||||
if (UnityPurchasesGetAdditionalParamsCallbackSK2) {
|
||||
NSError *error = nil;
|
||||
NSData *jsonData = [NSJSONSerialization dataWithJSONObject:input options:0 error:&error];
|
||||
if (error || !jsonData) {
|
||||
NSLog(@"[AppsFlyer] Failed to serialize Unity purchase data: %@", error);
|
||||
return @{};
|
||||
}
|
||||
|
||||
NSString *jsonString = [[NSString alloc] initWithData:jsonData encoding:NSUTF8StringEncoding];
|
||||
|
||||
const char *resultCStr = UnityPurchasesGetAdditionalParamsCallbackSK2([jsonString UTF8String], "");
|
||||
if (!resultCStr) {
|
||||
NSLog(@"[AppsFlyer] Unity callback returned null");
|
||||
return @{};
|
||||
}
|
||||
|
||||
NSString *resultJson = [NSString stringWithUTF8String:resultCStr];
|
||||
|
||||
NSData *resultData = [resultJson dataUsingEncoding:NSUTF8StringEncoding];
|
||||
NSDictionary *parsedResult = [NSJSONSerialization JSONObjectWithData:resultData options:0 error:&error];
|
||||
|
||||
if (error || ![parsedResult isKindOfClass:[NSDictionary class]]) {
|
||||
NSLog(@"[AppsFlyer] Failed to parse Unity response: %@", error);
|
||||
return @{};
|
||||
}
|
||||
|
||||
return parsedResult;
|
||||
} else {
|
||||
NSLog(@"[AppsFlyer] SK2 - Unity callback is NOT registered");
|
||||
}
|
||||
} else {
|
||||
NSLog(@"[AppsFlyer] SK2 - iOS version not supported");
|
||||
}
|
||||
return @{};
|
||||
}
|
||||
|
||||
@end
|
||||
|
||||
|
||||
|
||||
Reference in New Issue
Block a user