-
Notifications
You must be signed in to change notification settings - Fork 447
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add SwiftUI lifecycle install example code #9721
Changes from all commits
c0e2b6f
caad472
d63e1c0
908e7d2
56092bd
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
@@ -0,0 +1,28 @@ | ||||||
```swift | ||||||
import SwiftUI | ||||||
import PostHog | ||||||
|
||||||
@main | ||||||
struct YourGreatApp: App { | ||||||
|
||||||
// Add PostHog to your app's initializer. | ||||||
// If using UIApplicationDelegateAdaptor, see the UIKit tab. | ||||||
|
||||||
init() { | ||||||
|
||||||
let POSTHOG_API_KEY = "<ph_project_api_key>" | ||||||
// usually 'https://us.i.posthog.com' or 'https://eu.i.posthog.com' | ||||||
let POSTHOG_HOST = "<ph_client_api_host>" | ||||||
|
||||||
let config = PostHogConfig(apiKey: POSTHOG_API_KEY, host: POSTHOG_HOST) | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
docs render the There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @daniloc I think you missed this one, one of them has the TIP and the other one not There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @marandaneto oop, automerge snared me before I saw this. I will fix! |
||||||
PostHogSDK.shared.setup(config) | ||||||
|
||||||
} | ||||||
|
||||||
var body: some Scene { | ||||||
WindowGroup { | ||||||
ContentView() | ||||||
} | ||||||
} | ||||||
} | ||||||
``` |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
```swift | ||
import Foundation | ||
import PostHog | ||
import UIKit | ||
|
||
class AppDelegate: NSObject, UIApplicationDelegate { | ||
func application(_: UIApplication, didFinishLaunchingWithOptions _: [UIApplication.LaunchOptionsKey: Any]? = nil) -> Bool { | ||
let POSTHOG_API_KEY = "<ph_project_api_key>" | ||
// usually 'https://us.i.posthog.com' or 'https://eu.i.posthog.com' | ||
let POSTHOG_HOST = "<ph_client_api_host>" | ||
|
||
let config = PostHogConfig(apiKey: POSTHOG_API_KEY, host: POSTHOG_HOST) // TIP: host is optional if you use https://us.i.posthog.com | ||
PostHogSDK.shared.setup(config) | ||
|
||
return true | ||
} | ||
} | ||
``` |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There are more ways to do this, for example:
and then:
Maybe add a comment that they can also use (or already use) the
UIApplicationDelegateAdaptor
, they can place the code inside theapplication
method? or even another tab using the delegate?@ioannisj might have other ideas.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Oh I do like the thought of one more tab so we cover all the common cases:
Minimal mental friction for whichever setup you’re using.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Okay I will work this up later today, test it, and add it in, tysm!
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think just mentioning
UIApplicationDelegate
andSwiftUI app init()
should be enough for a developer. Developers who useUIApplicationDelegate
in their SwiftUI apps already know how to connect the two - plus there are so many ways to bootstrap an iOS app that I think it's not practical to cover them allThere was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We can maybe mention that SDK init should go in your app init code, which is usually...
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
How about amending the comment in the above screenshot to read:
Would that get us there?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yeah, I think I prefer this alternative actually 👍
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Sold! 👨🏻⚖️
Thank you both!