Skip to content

Commit

Permalink
fix
Browse files Browse the repository at this point in the history
  • Loading branch information
marandaneto committed Jan 10, 2024
1 parent 4a206d3 commit 3e113cb
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 35 deletions.
49 changes: 19 additions & 30 deletions PostHog/PostHogSDK.swift
Original file line number Diff line number Diff line change
Expand Up @@ -641,15 +641,15 @@ let maxRetryDelay = 30.0

#if os(iOS) || os(tvOS)
defaultCenter.addObserver(self,
selector: #selector(captureAppLifecycle),
selector: #selector(captureAppInstallLifecycle),
name: UIApplication.didFinishLaunchingNotification,
object: nil)
defaultCenter.addObserver(self,
selector: #selector(captureAppBackgrounded),
name: UIApplication.didEnterBackgroundNotification,
object: nil)
defaultCenter.addObserver(self,
selector: #selector(captureAppOpenedFromBackground),
selector: #selector(captureAppOpened),
name: UIApplication.didBecomeActiveNotification,
object: nil)
#endif
Expand All @@ -663,7 +663,11 @@ let maxRetryDelay = 30.0
}
}

func captureAppInstalled() {
@objc func captureAppInstallLifecycle() {
if !config.captureApplicationLifecycleEvents {
return
}

let bundle = Bundle.main

let versionName = bundle.infoDictionary?["CFBundleShortVersionString"] as? String
Expand Down Expand Up @@ -718,44 +722,29 @@ let maxRetryDelay = 30.0
}
}

func captureAppOpened() {
@objc 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
props["from_background"] = appFromBackground

if versionName != nil {
props["version"] = versionName
}
if versionCode != nil {
props["build"] = versionCode
}
if !appFromBackground {
let bundle = Bundle.main

capture("Application Opened", properties: props)
}
let versionName = bundle.infoDictionary?["CFBundleShortVersionString"] as? String
let versionCode = bundle.infoDictionary?["CFBundleVersion"] as? String

@objc func captureAppOpenedFromBackground() {
var props: [String: Any] = [:]
props["from_background"] = appFromBackground
if versionName != nil {
props["version"] = versionName
}
if versionCode != nil {
props["build"] = versionCode
}

if !appFromBackground {
appFromBackground = true
}

capture("Application Opened", properties: props)
}

@objc private func captureAppLifecycle() {
if !config.captureApplicationLifecycleEvents {
return
}

captureAppInstalled()
}

@objc func captureAppBackgrounded() {
if !config.captureApplicationLifecycleEvents {
return
Expand Down
10 changes: 5 additions & 5 deletions PostHogTests/PostHogSDKTest.swift
Original file line number Diff line number Diff line change
Expand Up @@ -301,7 +301,7 @@ class PostHogSDKTest: QuickSpec {
it("capture AppInstalled") {
let sut = self.getSut()

sut.captureAppInstalled()
sut.captureAppInstallLifecycle()

let events = getBatchedEvents(server)

Expand All @@ -324,7 +324,7 @@ class PostHogSDKTest: QuickSpec {
userDefaults.setValue("1", forKey: "PHGBuildKeyV2")
userDefaults.synchronize()

sut.captureAppInstalled()
sut.captureAppInstallLifecycle()

let events = getBatchedEvents(server)

Expand All @@ -344,7 +344,7 @@ class PostHogSDKTest: QuickSpec {
it("capture AppOpenedFromBackground from_background should be false") {
let sut = self.getSut()

sut.captureAppOpenedFromBackground()
sut.captureAppOpened()

let events = getBatchedEvents(server)

Expand All @@ -361,8 +361,8 @@ class PostHogSDKTest: QuickSpec {
it("capture AppOpenedFromBackground from_background should be true") {
let sut = self.getSut(flushAt: 2)

sut.captureAppOpenedFromBackground()
sut.captureAppOpenedFromBackground()
sut.captureAppOpened()
sut.captureAppOpened()

let events = getBatchedEvents(server)

Expand Down

0 comments on commit 3e113cb

Please sign in to comment.