-
Notifications
You must be signed in to change notification settings - Fork 11
/
AppsFlyerXApple.mm
468 lines (372 loc) · 19.9 KB
/
AppsFlyerXApple.mm
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
//
// AppsFlyerX.mm
// AppsFlyerCocos2dX
//
// Created by Andrey Gagan
// Copyright © 2017 AppsFlyer. All rights reserved.
//
#include "AppsFlyerX.h"
#include "AppsFlyerXApple.h"
#include "AppsFlyerXAppleHelper.h"
#include "AppsFlyerXAppleDelegate.h"
#include "AppsFlyerXAppleDeepLinkDelegate.h"
#include "AFSDKXPurchaseDetails.h"
#include "AFSDKXValidateAndLogResult.h"
#import "libAppsFlyer/AppsFlyerLib.h"
#import "libAppsFlyer/AFSDKPurchaseDetails.h"
#import <UIKit/UIKit.h>
/* Null, because instance will be initialized on demand. */
AppsFlyerXApple* AppsFlyerXApple::instance = 0;
AppsFlyerXApple* AppsFlyerXApple::getInstance() {
if (instance == 0) {
instance = new AppsFlyerXApple();
}
return instance;
}
//
//class AppsFlyerXAppleDeepLinkResult() {
// public:
// (instancetype)initWithDeepLink:(id)deeplink error:(NSError *)error;
//}
//
//class AppsFlyerXAppleDeepLink(){
// + (instancetype _Nullable)withParameters:(NSDictionary * _Nullable)parameters;
// + (instancetype _Nullable)withOneLink:(NSDictionary * _Nullable)parameters;
//}
AppsFlyerXApple::AppsFlyerXApple() {}
void AppsFlyerXApple::enableTCFDataCollection(bool shouldCollectConsentData){
[[AppsFlyerLib shared] enableTCFDataCollection:shouldCollectConsentData];
}
void AppsFlyerXApple::setConsentData(const AppsFlyerXConsent& consentData){
if (consentData.IsUserSubjectToGDPR()){
[[AppsFlyerLib shared] setConsentData:[[AppsFlyerConsent alloc] initForGDPRUserWithHasConsentForDataUsage:consentData.HasConsentForDataUsage() hasConsentForAdsPersonalization:consentData.HasConsentForAdsPersonalization()]];
}
else{
[[AppsFlyerLib shared] setConsentData:[[AppsFlyerConsent alloc] initNonGDPRUser]];
}
}
void AppsFlyerXApple::setCustomerUserID(const std::string& customerUserID) {
[[AppsFlyerLib shared] setCustomerUserID: [NSString stringWithUTF8String:customerUserID.c_str()]];
}
std::string AppsFlyerXApple::customerUserID(){
return [[[AppsFlyerLib shared] customerUserID] UTF8String];
}
void AppsFlyerXApple::setAdditionalData(cocos2d::ValueMap customData) {
[[AppsFlyerLib shared] setAdditionalData: AppsFlyerXAppleHelper::valueMap2nsDictionary(customData)];
}
cocos2d::ValueMap AppsFlyerXApple::customData() {
return AppsFlyerXAppleHelper::nsDictionary2ValueMap([[AppsFlyerLib shared] customData]);
}
void AppsFlyerXApple::setAppsFlyerDevKey(const std::string& appsFlyerDevKey) {
[[AppsFlyerLib shared] setAppsFlyerDevKey:[NSString stringWithUTF8String:appsFlyerDevKey.c_str()]];
static dispatch_once_t onceToken;
static AppsFlyerXApple *xApple = nil;
static AppsFlyerXAppleDelegate *delegate = nil;
[[AppsFlyerLib shared] setPluginInfoWith: AFSDKPluginCocos2dx
pluginVersion:@"6.15.3"
additionalParams:nil];
dispatch_once(&onceToken, ^{
xApple = AppsFlyerXApple::getInstance();
delegate = [[AppsFlyerXAppleDelegate alloc] init];
xApple->delegate = delegate;
[[NSNotificationCenter defaultCenter] addObserverForName: UIApplicationDidBecomeActiveNotification
object: nil
queue: nil
usingBlock: ^ (NSNotification * note) {
if (AppsFlyerX::manualStart == false) {
[[AppsFlyerLib shared] start];
}
}];
});
[[AppsFlyerLib shared] setDelegate: delegate];
}
std::string AppsFlyerXApple::appsFlyerDevKey() {
return [[[AppsFlyerLib shared] appsFlyerDevKey] UTF8String];
}
void AppsFlyerXApple::setAppleAppID(const std::string& appleAppID) {
[[AppsFlyerLib shared] setAppleAppID:[NSString stringWithUTF8String:appleAppID.c_str()]];
}
std::string AppsFlyerXApple::appleAppID() {
return [[[AppsFlyerLib shared] appleAppID] UTF8String];
}
void AppsFlyerXApple::setCurrencyCode(const std::string& currencyCode) {
[[AppsFlyerLib shared] setCurrencyCode:[NSString stringWithUTF8String:currencyCode.c_str()]];
}
std::string AppsFlyerXApple::currencyCode() {
return [[[AppsFlyerLib shared] currencyCode] UTF8String];
}
#ifndef AFSDK_NO_IDFA
void AppsFlyerXApple::disableAdvertisingIdentifier(bool shouldDisable) {
[[AppsFlyerLib shared] setDisableAdvertisingIdentifier:shouldDisable];
}
#endif
#ifndef AFSDK_NO_IDFA
bool AppsFlyerXApple::isDisabledAdvertisingIdentifier() {
return [[AppsFlyerLib shared] disableAdvertisingIdentifier];
}
#endif
void AppsFlyerXApple::setIsDebug(bool isDebug) {
[[AppsFlyerLib shared] setIsDebug:isDebug];
}
bool AppsFlyerXApple::isDebug() {
return [[AppsFlyerLib shared] isDebug];
}
void AppsFlyerXApple::setShouldCollectDeviceName(bool isShouldCollectDeviceName) {
[[AppsFlyerLib shared] setShouldCollectDeviceName:isShouldCollectDeviceName];
}
bool AppsFlyerXApple::isShouldCollectDeviceName() {
return [[AppsFlyerLib shared] shouldCollectDeviceName];
}
void AppsFlyerXApple::setAppInviteOneLink(const std::string& appInviteOneLinkID) {
return [[AppsFlyerLib shared] setAppInviteOneLink:[NSString stringWithUTF8String:appInviteOneLinkID.c_str()]];
}
std::string AppsFlyerXApple::appInviteOneLinkID() {
return [[[AppsFlyerLib shared] appInviteOneLinkID] UTF8String];
}
void AppsFlyerXApple::anonymizeUser(bool shouldAnonymize) {
[[AppsFlyerLib shared] setAnonymizeUser:shouldAnonymize];
}
bool AppsFlyerXApple::isAnonymizedUser() {
return [[AppsFlyerLib shared] anonymizeUser];
}
void AppsFlyerXApple::setDisableCollectASA(bool shouldDisable) {
[[AppsFlyerLib shared] setDisableCollectASA:shouldDisable];
}
bool AppsFlyerXApple::isDisabledCollectASA() {
return [[AppsFlyerLib shared] disableCollectASA];
}
void AppsFlyerXApple::setUseReceiptValidationSandbox(bool useReceiptValidationSandbox) {
[[AppsFlyerLib shared] setUseReceiptValidationSandbox:useReceiptValidationSandbox];
}
bool AppsFlyerXApple::isUseReceiptValidationSandbox() {
return [[AppsFlyerLib shared] useReceiptValidationSandbox];
}
void AppsFlyerXApple::setUseUninstallSandbox(bool setUseUninstallSandbox) {
[[AppsFlyerLib shared] setUseUninstallSandbox:setUseUninstallSandbox];
}
bool AppsFlyerXApple::isUseUninstallSandbox() {
return [[AppsFlyerLib shared] useUninstallSandbox];
}
void AppsFlyerXApple::setUserEmails(std::vector<std::string> userEmails, EmailCryptTypeX type) {
NSMutableArray *emails = [NSMutableArray new];
for (auto userEmail : userEmails) {
[emails addObject:[NSString stringWithUTF8String:userEmail.c_str()]];
}
[[AppsFlyerLib shared] setUserEmails:emails withCryptType:(EmailCryptType)[@(type) integerValue]];
}
void AppsFlyerXApple::start() {
[[AppsFlyerLib shared] start];
}
void AppsFlyerXApple::logEvent(const std::string& eventName, cocos2d::ValueMap values) {
NSMutableDictionary *dictionary = AppsFlyerXAppleHelper::valueMap2nsDictionary(values).mutableCopy;
NSString *event = [NSString stringWithUTF8String:eventName.c_str()];
id revenueString = dictionary[@"af_revenue"];
if (revenueString && [revenueString isKindOfClass:[NSString class]]) {
NSNumberFormatter *formatter = [[NSNumberFormatter alloc] init];
formatter.numberStyle = NSNumberFormatterDecimalStyle;
NSNumber *revenueNumber = [formatter numberFromString:revenueString];
dictionary[@"af_revenue"] = revenueNumber;
}
[[AppsFlyerLib shared] logEvent:event withValues:dictionary];
}
void AppsFlyerXApple::validateAndLogInAppPurchase(const std::string& productIdentifier,
const std::string& price,
const std::string& currency,
const std::string& tranactionId,
cocos2d::ValueMap params,
std::function<void(cocos2d::ValueMap)> successBlock,
std::function<void(cocos2d::ValueMap)> failureBlock) {
NSString *lProductIdentifier = [NSString stringWithUTF8String:productIdentifier.c_str()];
NSString *lPrice = [NSString stringWithUTF8String:price.c_str()];
NSString *lCurrency = [NSString stringWithUTF8String:currency.c_str()];
NSString *lTranactionId = [NSString stringWithUTF8String:tranactionId.c_str()];
NSDictionary *lParams = AppsFlyerXAppleHelper::valueMap2nsDictionary(params);
[[AppsFlyerLib shared] validateAndLogInAppPurchase:lProductIdentifier
price:lPrice
currency:lCurrency
transactionId:lTranactionId
additionalParameters:lParams success:^(NSDictionary *response) {
cocos2d::ValueMap lResponce = AppsFlyerXAppleHelper::nsDictionary2ValueMap(response);
successBlock(lResponce);
} failure:^(NSError *error, id response) {
NSMutableDictionary *errorDictionary = @{}.mutableCopy;
if (error) {
errorDictionary[@"errorCode"] = [NSNumber numberWithInteger:error.code];
errorDictionary[@"errorDescription"] = error.localizedDescription;
}
if (response && [response isKindOfClass:[NSString class]]) {
errorDictionary[@"errorCode"] = [NSNumber numberWithInteger:-1];
errorDictionary[@"errorDescription"] = response;
}
failureBlock(AppsFlyerXAppleHelper::nsDictionary2ValueMap(errorDictionary));
}];
}
void AppsFlyerXApple::validateAndLogInAppPurchase(AFSDKXPurchaseDetails &details,
cocos2d::ValueMap params,
std::function<void(AFSDKXValidateAndLogResult)> completionHandler) {
NSString *productId = [NSString stringWithUTF8String:details.getProductId().c_str()];
NSString *price = [NSString stringWithUTF8String:details.getPrice().c_str()];
NSString *transactionId = [NSString stringWithUTF8String:details.getTransactionId().c_str()];
NSString *currency = [NSString stringWithUTF8String:details.getCurrency().c_str()];
NSDictionary *lParams = AppsFlyerXAppleHelper::valueMap2nsDictionary(params);
AFSDKPurchaseDetails *afPurchaseDetails = [[AFSDKPurchaseDetails alloc] initWithProductId:productId
price:price
currency:currency
transactionId:transactionId];
[[AppsFlyerLib shared] validateAndLogInAppPurchase:afPurchaseDetails extraEventValues:lParams completionHandler:^(AFSDKValidateAndLogResult * _Nullable result) {
// TODO: - add result to completionHandler
NSLog(@"[ValidateAndLog] Result: %@", result);
AFSDKXValidateAndLogStatus status = AFSDKXValidateAndLogResult::objcEnumToCppEnum(result.status);
cocos2d::ValueMap resultX = AppsFlyerXAppleHelper::nsDictionary2ValueMap(result.result);
cocos2d::ValueMap errorData = AppsFlyerXAppleHelper::nsDictionary2ValueMap(result.errorData);
NSMutableDictionary *errorDictionary = [NSMutableDictionary dictionary];
if (result.error) {
errorDictionary[@"errorCode"] = [NSNumber numberWithInteger:result.error.code];
errorDictionary[@"errorDescription"] = result.error.localizedDescription;
}
AFSDKXValidateAndLogResult validateResult = AFSDKXValidateAndLogResult(status, resultX, errorData, AppsFlyerXAppleHelper::nsDictionary2ValueMap(errorDictionary));
completionHandler(validateResult);
}];
}
void AppsFlyerXApple::logAdRevenue(AFXAdRevenueData adRevenueData,
cocos2d::ValueMap additionalParameters) {
NSDictionary *lParams = AppsFlyerXAppleHelper::valueMap2nsDictionary(additionalParameters);
NSString * monetizationNetwork = [NSString stringWithUTF8String:adRevenueData.getMonetizationNetwork().c_str()];
AppsFlyerAdRevenueMediationNetworkType mediationNetwork = static_cast<AppsFlyerAdRevenueMediationNetworkType>(adRevenueData.getMediationNetwork());
NSString * currencyIso4217Code = [NSString stringWithUTF8String:adRevenueData.getCurrencyIso4217Code().c_str()];
NSNumber *eventRevenue = [NSNumber numberWithDouble:adRevenueData.getEventRevenue()];
AFAdRevenueData *appsflyerAdRevenueData = [[AFAdRevenueData alloc] initWithMonetizationNetwork:monetizationNetwork mediationNetwork:mediationNetwork currencyIso4217Code:currencyIso4217Code eventRevenue:eventRevenue];
[[AppsFlyerLib shared] logAdRevenue:appsflyerAdRevenueData additionalParameters:lParams];
}
void AppsFlyerXApple::logLocation(double longitude, double latitude) {
[[AppsFlyerLib shared] logLocation:longitude latitude:latitude];
}
std::string AppsFlyerXApple::getAppsFlyerUID() {
return [[[AppsFlyerLib shared] getAppsFlyerUID] UTF8String];
}
void AppsFlyerXApple::handleOpenURL(const std::string& url, const std::string& sourceApplication) {
NSURL *nsurl = [NSURL URLWithString:[NSString stringWithUTF8String:url.c_str()]];
[[AppsFlyerLib shared] handleOpenURL:nsurl
sourceApplication:[NSString stringWithUTF8String:sourceApplication.c_str()]];
}
void AppsFlyerXApple::handleOpenURL(std::string url, std::string sourceApplication, void* annotation) {
// annotation unused
NSURL *nsurl = [NSURL URLWithString:[NSString stringWithUTF8String:url.c_str()]];
[[AppsFlyerLib shared] handleOpenURL:nsurl sourceApplication:[NSString stringWithUTF8String:sourceApplication.c_str()] withAnnotation:nil];
}
void AppsFlyerXApple::handleOpenURL(std::string url, cocos2d::ValueMap options) {
NSURL *nsurl = [NSURL URLWithString:[NSString stringWithUTF8String:url.c_str()]];
[[AppsFlyerLib shared] handleOpenUrl:nsurl options:AppsFlyerXAppleHelper::valueMap2nsDictionary(options)];
}
/*
- (BOOL)continueUserActivity:(NSUserActivity *)userActivity restorationHandler:(void (^)(NSArray *))restorationHandler;
- (void)didUpdateUserActivity:(NSUserActivity *)userActivity NS_AVAILABLE_IOS(9_0);
*/
void AppsFlyerXApple::handlePushNotification(cocos2d::ValueMap pushPayload) {
[[AppsFlyerLib shared] handlePushNotification: AppsFlyerXAppleHelper::valueMap2nsDictionary(pushPayload)];
}
void AppsFlyerXApple::registerUninstall(void* deviceToken, unsigned long length) {
[[AppsFlyerLib shared] registerUninstall: [NSData dataWithBytes:deviceToken length:length]];
}
void AppsFlyerXApple::remoteDebuggingCallWithData(const std::string& data) {
[[AppsFlyerLib shared] remoteDebuggingCallWithData:[NSString stringWithUTF8String:data.c_str()]];
}
void AppsFlyerXApple::setHost(const std::string& host) {
return [[AppsFlyerLib shared] setHost:[NSString stringWithUTF8String:host.c_str()]];
}
std::string AppsFlyerXApple::getHost() {
return [[[AppsFlyerLib shared] host] UTF8String];
}
void AppsFlyerXApple::setMinTimeBetweenSessions(unsigned long minTimeBetweenSessions) {
[[AppsFlyerLib shared] setMinTimeBetweenSessions:minTimeBetweenSessions];
}
unsigned long AppsFlyerXApple::getMinTimeBetweenSessions() {
return [[AppsFlyerLib shared] minTimeBetweenSessions];
}
// Delegates methods proxy
void AppsFlyerXApple::setOnConversionDataReceived(void(*callback)(cocos2d::ValueMap installData)) {
static_cast<AppsFlyerXAppleDelegate *>(AppsFlyerXApple::getInstance()->delegate).onConversionDataReceivedCallback = callback;
}
void AppsFlyerXApple::setOnConversionDataRequestFailure(void(*callback)(cocos2d::ValueMap error)) {
static_cast<AppsFlyerXAppleDelegate *>(AppsFlyerXApple::getInstance()->delegate).onConversionDataRequestFailureCallback = callback;
}
void AppsFlyerXApple::setOnAppOpenAttribution(void(*callback)(cocos2d::ValueMap attributionData)) {
static_cast<AppsFlyerXAppleDelegate *>(AppsFlyerXApple::getInstance()->delegate).onAppOpenAttributionCallback = callback;
}
void AppsFlyerXApple::setOnAppOpenAttributionFailure(void(*callback)(cocos2d::ValueMap error)) {
static_cast<AppsFlyerXAppleDelegate *>(AppsFlyerXApple::getInstance()->delegate).onAppOpenAttributionFailureCallback = callback;
}
void AppsFlyerXApple::stop(bool stop) {
[[AppsFlyerLib shared] setIsStopped:stop];
}
void AppsFlyerXApple::sharingFilter(std::vector<std::string> partners){
NSMutableArray *filteredPartners = [NSMutableArray new];
for (auto partner : partners) {
[filteredPartners addObject:[NSString stringWithUTF8String:partner.c_str()]];
}
[[AppsFlyerLib shared] setSharingFilter:filteredPartners];
}
void AppsFlyerXApple::sharingFilterForAllPartners() {
[[AppsFlyerLib shared] setSharingFilterForAllPartners];
}
void AppsFlyerXApple::disableSKAdNetwork(bool shouldDisable){
[[AppsFlyerLib shared] setDisableSKAdNetwork:shouldDisable];
}
bool AppsFlyerXApple::isDisabledSKAdNetwork(){
return [[AppsFlyerLib shared] disableSKAdNetwork];
}
#ifndef AFSDK_NO_IDFA
void AppsFlyerXApple::waitForATTUserAuthorizationWithTimeoutInterval(double timeoutInterval){
if (@available(iOS 14, *)) {
[[AppsFlyerLib shared] waitForATTUserAuthorizationWithTimeoutInterval:timeoutInterval];
}
}
#endif
void AppsFlyerXApple::setPhoneNumber(const std::string& phoneNumber){
NSString *phone = [NSString stringWithUTF8String:phoneNumber.c_str()];
[[AppsFlyerLib shared] setPhoneNumber:phone];
}
void AppsFlyerXApple::setDidResolveDeepLink(void(*callback)(AppsFlyerXDeepLinkResult result)) {
static dispatch_once_t onceToken;
static AppsFlyerXApple *xApple = nil;
static AppsFlyerXAppleDeepLinkDelegate *deepLinkDelegate = nil;
dispatch_once(&onceToken, ^{
xApple = AppsFlyerXApple::getInstance();
deepLinkDelegate = [[AppsFlyerXAppleDeepLinkDelegate alloc] init];
xApple->deepLinkDelegate = deepLinkDelegate;
});
[[AppsFlyerLib shared] setDeepLinkDelegate: deepLinkDelegate];
static_cast<AppsFlyerXAppleDeepLinkDelegate *>(AppsFlyerXApple::getInstance()->deepLinkDelegate).didResolveDeepLinkCallback = callback;
}
void AppsFlyerXApple::setPartnerData(const std::string& partnerId, cocos2d::ValueMap data){
[[AppsFlyerLib shared] setPartnerDataWithPartnerId:[NSString stringWithUTF8String:partnerId.c_str()] partnerInfo:AppsFlyerXAppleHelper::valueMap2nsDictionary(data)];
}
void AppsFlyerXApple::setOneLinkCustomDomains(std::vector<std::string> domains){
NSMutableArray *customDomains = [NSMutableArray new];
for (auto domain : domains) {
[customDomains addObject:[NSString stringWithUTF8String:domain.c_str()]];
}
[AppsFlyerLib shared].oneLinkCustomDomains = customDomains;
}
void AppsFlyerXApple::setCurrentDeviceLanguage(const std::string& language) {
[AppsFlyerLib shared].currentDeviceLanguage = [NSString stringWithUTF8String:language.c_str()];
}
void AppsFlyerXApple::setSharingFilterForPartners(std::vector<std::string> partners) {
NSMutableArray *sharingFilter = [NSMutableArray new];
for (auto partner : partners) {
[sharingFilter addObject:[NSString stringWithUTF8String:partner.c_str()]];
}
[[AppsFlyerLib shared] setSharingFilterForPartners: sharingFilter];
}
void AppsFlyerXApple::logInvite(const std::string& channel, cocos2d::ValueMap parameters) {
NSString *lChannel = [NSString stringWithUTF8String:channel.c_str()];
[AppsFlyerShareInviteHelper logInvite:lChannel parameters:AppsFlyerXAppleHelper::valueMap2nsDictionary(parameters)];
}
void AppsFlyerXApple::generateUserInviteLink(cocos2d::ValueMap parameters, std::function<void(std::string url)> callback) {
[AppsFlyerShareInviteHelper generateInviteUrlWithLinkGenerator:^AppsFlyerLinkGenerator * _Nonnull(AppsFlyerLinkGenerator * _Nonnull generator) {
return AppsFlyerXAppleHelper::valueMap2LinkGenerator(parameters, generator);
} completionHandler:^(NSURL * _Nullable url) {
NSLog(@"url is %@", [url absoluteString]);
callback([url.absoluteString UTF8String]);
}];
};