diff options
author | Alex Zolotarev <alex@maps.me> | 2015-05-19 18:22:46 +0300 |
---|---|---|
committer | Alex Zolotarev <alex@maps.me> | 2015-09-23 02:49:15 +0300 |
commit | 9d19ff9158f652bd2cf030e098c3d2287814b917 (patch) | |
tree | 3c55e38713f44465c1b60f38cd73e00b77d53271 /3party/Alohalytics/src/apple/alohalytics_objc.mm | |
parent | 58a66a2f358b4230f4cda92d1fe7d80a737907dd (diff) |
[ios][alohalytics] Built-in processing of basic app lifecycle events.
@TODO(AlexZ): Refactor stats engine to allow synchronous uploading on the app background.
Diffstat (limited to '3party/Alohalytics/src/apple/alohalytics_objc.mm')
-rw-r--r-- | 3party/Alohalytics/src/apple/alohalytics_objc.mm | 64 |
1 files changed, 64 insertions, 0 deletions
diff --git a/3party/Alohalytics/src/apple/alohalytics_objc.mm b/3party/Alohalytics/src/apple/alohalytics_objc.mm index b9812feebb..4ee686d0ca 100644 --- a/3party/Alohalytics/src/apple/alohalytics_objc.mm +++ b/3party/Alohalytics/src/apple/alohalytics_objc.mm @@ -266,6 +266,13 @@ static alohalytics::TStringMap ParseLaunchOptions(NSDictionary * options) { } return parsed; } + +// ********* Objective-C implementation part. ********* +// We process all events on a non-main thread. +static dispatch_queue_t sBackgroundThreadQueue = nil; +// Need it to effectively upload data when app goes into the background. +static UIBackgroundTaskIdentifier sBackgroundTaskId = UIBackgroundTaskInvalid; + #endif // TARGET_OS_IPHONE } // namespace @@ -280,6 +287,18 @@ static alohalytics::TStringMap ParseLaunchOptions(NSDictionary * options) { } + (void)setup:(NSString *)serverUrl andFirstLaunch:(BOOL)isFirstLaunch withLaunchOptions:(NSDictionary *)options { + +#if (TARGET_OS_IPHONE > 0) + // Subscribe to basic app lifecycle events. + sBackgroundThreadQueue = ::dispatch_queue_create([serverUrl UTF8String], DISPATCH_QUEUE_SERIAL); + NSNotificationCenter * nc = [NSNotificationCenter defaultCenter]; + Class cls = [Alohalytics class]; + [nc addObserver:cls selector:@selector(applicationDidBecomeActive:) name:UIApplicationDidBecomeActiveNotification object:nil]; + [nc addObserver:cls selector:@selector(applicationWillResignActive:) name:UIApplicationWillResignActiveNotification object:nil]; + [nc addObserver:cls selector:@selector(applicationWillEnterForeground:) name:UIApplicationWillEnterForegroundNotification object:nil]; + [nc addObserver:cls selector:@selector(applicationDidEnterBackground:) name:UIApplicationDidEnterBackgroundNotification object:nil]; + [nc addObserver:cls selector:@selector(applicationWillTerminate:) name:UIApplicationWillTerminateNotification object:nil]; +#endif // TARGET_OS_IPHONE const auto installationId = InstallationId(); Stats & instance = Stats::Instance(); instance.SetClientId(installationId.first) @@ -366,4 +385,49 @@ static alohalytics::TStringMap ParseLaunchOptions(NSDictionary * options) { Stats::Instance().LogEvent(ToStdString(event), ToStringMap(dictionary), ExtractLocation(location)); } +#pragma mark App lifecycle notifications used to calculate basic metrics. +#if (TARGET_OS_IPHONE > 0) ++ (void)applicationDidBecomeActive:(NSNotification *)notification { + Stats::Instance().LogEvent("$applicationDidBecomeActive"); +} + ++ (void)applicationWillResignActive:(NSNotification *)notification { + Stats::Instance().LogEvent("$applicationWillResignActive"); +} + ++ (void)endBackgroundTask { + [[UIApplication sharedApplication] endBackgroundTask:sBackgroundTaskId]; + sBackgroundTaskId = UIBackgroundTaskInvalid; +} + ++ (void)applicationWillEnterForeground:(NSNotificationCenter *)notification { + Stats::Instance().LogEvent("$applicationWillEnterForeground"); + + ::dispatch_async(sBackgroundThreadQueue, ^{ + if (sBackgroundTaskId != UIBackgroundTaskInvalid) { + [Alohalytics endBackgroundTask]; + } + }); +} + ++ (void)applicationDidEnterBackground:(NSNotification *)notification { + Stats::Instance().LogEvent("$applicationDidEnterBackground"); + + sBackgroundTaskId = [[UIApplication sharedApplication] beginBackgroundTaskWithExpirationHandler:^{ + [Alohalytics endBackgroundTask]; + }]; + + ::dispatch_async(sBackgroundThreadQueue, ^{ + // TODO: Here upload should be refactored and called synchronously to work correctly. + //alohalytics::Stats::Instance().Upload(); + if (sBackgroundTaskId != UIBackgroundTaskInvalid) { + [Alohalytics endBackgroundTask]; + } + }); +} + ++ (void)applicationWillTerminate:(NSNotification *)notification { + Stats::Instance().LogEvent("$applicationWillTerminate"); +} +#endif // TARGET_OS_IPHONE @end |