From 711ce92549069cab2a5323c6173456ee87ca3470 Mon Sep 17 00:00:00 2001 From: Manoel Aranda Neto Date: Mon, 23 Oct 2023 13:16:25 +0200 Subject: [PATCH] fix lifecycle issues --- .swiftlint.yml | 8 +++---- PostHog/PostHogSDK.swift | 48 ++++++++++++++++++++++++++++++++++------ 2 files changed, 45 insertions(+), 11 deletions(-) diff --git a/.swiftlint.yml b/.swiftlint.yml index cf3bbe72b..2bb918609 100644 --- a/.swiftlint.yml +++ b/.swiftlint.yml @@ -22,9 +22,9 @@ file_length: error: 1200 function_body_length: - - 300 # warning - - 400 # error + - 1000 # warning + - 1200 # error type_body_length: - - 500 # warning - - 600 # error + - 1000 # warning + - 1200 # error diff --git a/PostHog/PostHogSDK.swift b/PostHog/PostHogSDK.swift index d63230fad..1068c28ea 100644 --- a/PostHog/PostHogSDK.swift +++ b/PostHog/PostHogSDK.swift @@ -38,6 +38,7 @@ let maxRetryDelay = 30.0 private var context: PostHogContext? private static var apiKeys = Set() private var capturedAppInstalled = false + private var appFromBackground = false @objc public static let shared: PostHogSDK = { let instance = PostHogSDK(PostHogConfig(apiKey: "")) @@ -544,12 +545,27 @@ let maxRetryDelay = 30.0 let defaultCenter = NotificationCenter.default #if os(iOS) || os(tvOS) - let didFinishLaunchingNotification = UIApplication.didFinishLaunchingNotification - - defaultCenter.addObserver(self, selector: #selector(captureAppLifecycle), name: didFinishLaunchingNotification, object: nil) + defaultCenter.addObserver(self, + selector: #selector(captureAppLifecycle), + name: UIApplication.didFinishLaunchingNotification, + object: nil) + defaultCenter.addObserver(self, + selector: #selector(captureAppBackgrounded), + name: UIApplication.didEnterBackgroundNotification, + object: nil) + defaultCenter.addObserver(self, + selector: #selector(captureAppOpenedFromBackground), + name: UIApplication.willEnterForegroundNotification, + object: nil) #endif } + private func captureScreenViews() { + if config.captureScreenViews { +// swizzle(selector: #selector(), with: <#T##Selector#>, inClass: <#T##AnyClass#>, usingClass: <#T##AnyClass#>) + } + } + private func captureAppInstalled() { let bundle = Bundle.main @@ -606,27 +622,38 @@ let maxRetryDelay = 30.0 } private func captureAppOpened() { + var props: [String: Any] = [:] + props["from_background"] = false + let bundle = Bundle.main let versionName = bundle.infoDictionary?["CFBundleShortVersionString"] as? String let versionCode = bundle.infoDictionary?["CFBundleVersion"] as? String - var props: [String: Any] = [:] - if versionName != nil { props["version"] = versionName } if versionCode != nil { props["build"] = versionCode } -// TODO: detect info dynamically - props["from_background"] = false + // props["referring_application"] = launchOptions[UIApplicationLaunchOptionsSourceApplicationKey] // props["url"] = launchOptions[UIApplicationLaunchOptionsURLKey] capture("Application Opened", properties: props) } + @objc private func captureAppOpenedFromBackground() { + var props: [String: Any] = [:] + props["from_background"] = appFromBackground + + if !appFromBackground { + appFromBackground = true + } + + capture("Application Opened", properties: props) + } + @objc private func captureAppLifecycle() { if !config.captureApplicationLifecycleEvents { return @@ -635,4 +662,11 @@ let maxRetryDelay = 30.0 captureAppInstalled() captureAppOpened() } + + @objc private func captureAppBackgrounded() { + if !config.captureApplicationLifecycleEvents { + return + } + capture("Application Backgrounded") + } }