Skip to content

Commit

Permalink
fix lifecycle issues
Browse files Browse the repository at this point in the history
  • Loading branch information
marandaneto committed Oct 23, 2023
1 parent 3fc4849 commit 711ce92
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 11 deletions.
8 changes: 4 additions & 4 deletions .swiftlint.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
48 changes: 41 additions & 7 deletions PostHog/PostHogSDK.swift
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ let maxRetryDelay = 30.0
private var context: PostHogContext?
private static var apiKeys = Set<String>()
private var capturedAppInstalled = false
private var appFromBackground = false

@objc public static let shared: PostHogSDK = {
let instance = PostHogSDK(PostHogConfig(apiKey: ""))
Expand Down Expand Up @@ -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

Expand Down Expand Up @@ -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
Expand All @@ -635,4 +662,11 @@ let maxRetryDelay = 30.0
captureAppInstalled()
captureAppOpened()
}

@objc private func captureAppBackgrounded() {
if !config.captureApplicationLifecycleEvents {
return
}
capture("Application Backgrounded")
}
}

0 comments on commit 711ce92

Please sign in to comment.