react-native-appmetrica/ios/AppMetrica.m

172 lines
4.6 KiB
Mathematica
Raw Normal View History

2020-06-08 22:14:06 +03:00
/*
* Version for React Native
* © 2020 YANDEX
* You may not use this file except in compliance with the License.
* You may obtain a copy of the License at
* https://yandex.com/legal/appmetrica_sdk_agreement/
*/
2021-02-16 15:43:02 +03:00
#import <React/RCTConvert.h>
2020-06-08 22:14:06 +03:00
#import "AppMetrica.h"
2021-02-16 15:43:02 +03:00
#import <Firebase/Firebase.h>
2020-06-08 22:14:06 +03:00
#import "AppMetricaUtils.h"
2021-01-21 09:27:28 +03:00
#import <YandexMobileMetricaPush/YMPYandexMetricaPush.h>
2021-02-16 15:43:02 +03:00
2020-06-08 22:14:06 +03:00
static NSString *const kYMMReactNativeExceptionName = @"ReactNativeException";
@implementation AppMetrica
@synthesize methodQueue = _methodQueue;
2021-02-16 15:43:02 +03:00
RCT_EXPORT_MODULE();
- (id)init {
self = [super init];
return self;
}
- (dispatch_queue_t)methodQueue {
return dispatch_get_main_queue();
}
+ (BOOL)requiresMainQueueSetup {
return YES;
}
+ (NSDictionary *)addCustomPropsToUserProps:(NSDictionary *_Nullable)userProps withLaunchOptions:(NSDictionary *_Nullable)launchOptions {
NSMutableDictionary *appProperties = userProps != nil ? [userProps mutableCopy] : [NSMutableDictionary dictionary];
appProperties[@"isHeadless"] = @([RCTConvert BOOL:@(NO)]);
if (launchOptions[UIApplicationLaunchOptionsRemoteNotificationKey]) {
if ([UIApplication sharedApplication].applicationState == UIApplicationStateBackground) {
appProperties[@"isHeadless"] = @([RCTConvert BOOL:@(YES)]);
}
}
return [NSDictionary dictionaryWithDictionary:appProperties];
}
2020-06-08 22:14:06 +03:00
RCT_EXPORT_METHOD(activate:(NSDictionary *)configDict)
{
[YMMYandexMetrica activateWithConfiguration:[AppMetricaUtils configurationForDictionary:configDict]];
}
2021-01-21 11:23:32 +03:00
RCT_EXPORT_METHOD(reportUserProfile:(NSDictionary *)configDict)
{
[YMMYandexMetrica reportUserProfile:[AppMetricaUtils configurationForUserProfile:configDict] onFailure:^(NSError *error) {
NSLog(@"Error: %@", error);
}];
}
2021-02-16 15:43:02 +03:00
2021-01-21 11:23:32 +03:00
RCT_EXPORT_METHOD(initPush:(NSData *)deviceToken)
2021-01-21 09:27:28 +03:00
{
2021-02-16 15:43:02 +03:00
#ifdef DEBUG
YMPYandexMetricaPushEnvironment pushEnvironment = YMPYandexMetricaPushEnvironmentDevelopment;
#else
YMPYandexMetricaPushEnvironment pushEnvironment = YMPYandexMetricaPushEnvironmentProduction;
#endif
[YMPYandexMetricaPush setDeviceTokenFromData:[FIRMessaging messaging].APNSToken pushEnvironment:pushEnvironment];
2021-01-21 09:27:28 +03:00
}
2020-06-08 22:14:06 +03:00
RCT_EXPORT_METHOD(getLibraryApiLevel)
{
// It does nothing for iOS
}
RCT_EXPORT_METHOD(getLibraryVersion:(RCTPromiseResolveBlock)resolve rejecter:(RCTPromiseRejectBlock)reject)
{
resolve([YMMYandexMetrica libraryVersion]);
}
RCT_EXPORT_METHOD(pauseSession)
{
[YMMYandexMetrica pauseSession];
}
RCT_EXPORT_METHOD(reportAppOpen:(NSString *)deeplink)
{
[YMMYandexMetrica handleOpenURL:[NSURL URLWithString:deeplink]];
}
2021-02-16 15:43:02 +03:00
2020-06-08 22:14:06 +03:00
RCT_EXPORT_METHOD(reportError:(NSString *)message) {
NSException *exception = [[NSException alloc] initWithName:message reason:nil userInfo:nil];
[YMMYandexMetrica reportError:message exception:exception onFailure:NULL];
}
RCT_EXPORT_METHOD(reportEvent:(NSString *)eventName:(NSDictionary *)attributes)
{
if (attributes == nil) {
[YMMYandexMetrica reportEvent:eventName onFailure:^(NSError *error) {
NSLog(@"error: %@", [error localizedDescription]);
}];
} else {
[YMMYandexMetrica reportEvent:eventName parameters:attributes onFailure:^(NSError *error) {
NSLog(@"error: %@", [error localizedDescription]);
}];
}
}
RCT_EXPORT_METHOD(reportReferralUrl:(NSString *)referralUrl)
{
[YMMYandexMetrica reportReferralUrl:[NSURL URLWithString:referralUrl]];
}
RCT_EXPORT_METHOD(requestAppMetricaDeviceID:(RCTResponseSenderBlock)listener)
{
YMMAppMetricaDeviceIDRetrievingBlock completionBlock = ^(NSString *_Nullable appMetricaDeviceID, NSError *_Nullable error) {
listener(@[[self wrap:appMetricaDeviceID], [self wrap:[AppMetricaUtils stringFromRequestDeviceIDError:error]]]);
};
[YMMYandexMetrica requestAppMetricaDeviceIDWithCompletionQueue:nil completionBlock:completionBlock];
}
RCT_EXPORT_METHOD(resumeSession)
{
[YMMYandexMetrica resumeSession];
}
RCT_EXPORT_METHOD(sendEventsBuffer)
{
[YMMYandexMetrica sendEventsBuffer];
}
RCT_EXPORT_METHOD(setLocation:(NSDictionary *)locationDict)
{
[YMMYandexMetrica setLocation:[AppMetricaUtils locationForDictionary:locationDict]];
}
RCT_EXPORT_METHOD(setLocationTracking:(BOOL)enabled)
{
[YMMYandexMetrica setLocationTracking:enabled];
}
RCT_EXPORT_METHOD(setStatisticsSending:(BOOL)enabled)
{
[YMMYandexMetrica setStatisticsSending:enabled];
}
RCT_EXPORT_METHOD(setUserProfileID:(NSString *)userProfileID)
{
[YMMYandexMetrica setUserProfileID:userProfileID];
}
- (NSObject *)wrap:(NSObject *)value
{
if (value == nil) {
return [NSNull null];
}
return value;
}
@end