Skip to content
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

Merged
merged 5 commits into from
Oct 25, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 28 additions & 0 deletions contents/docs/integrate/_snippets/code-variants/ios-swiftui.mdx
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
```swift
import SwiftUI
import PostHog

@main
struct YourGreatApp: App {
Copy link
Member

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:

Suggested change
struct YourGreatApp: App {
struct YourGreatApp: App {
@UIApplicationDelegateAdaptor(AppDelegate.self) var appDelegate
init() {}

and then:

class AppDelegate: NSObject, UIApplicationDelegate {
    func application(_: UIApplication, didFinishLaunchingWithOptions _: [UIApplication.LaunchOptionsKey: Any]? = nil) -> Bool {
        // init posthog
        return true
    }
}

Maybe add a comment that they can also use (or already use) the UIApplicationDelegateAdaptor, they can place the code inside the application method? or even another tab using the delegate?

@ioannisj might have other ideas.

Copy link
Contributor Author

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:

  • UIKit
  • SwiftUI App init()
  • UIApplicationDelegateAdaptor

Minimal mental friction for whichever setup you’re using.

Copy link
Contributor Author

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!

Copy link
Contributor

@ioannisj ioannisj Oct 25, 2024

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 and SwiftUI app init() should be enough for a developer. Developers who use UIApplicationDelegate 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 all

Copy link
Contributor

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...

Copy link
Contributor Author

@daniloc daniloc Oct 25, 2024

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:

// Add PostHog to your app's initializer.
// If using UIApplicationDelegateAdaptor, see the UIKit tab.

Would that get us there?

Copy link
Contributor

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 👍

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sold! 👨🏻‍⚖️

Thank you both!


// 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)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
let config = PostHogConfig(apiKey: POSTHOG_API_KEY, host: POSTHOG_HOST)
let config = PostHogConfig(apiKey: POSTHOG_API_KEY, host: POSTHOG_HOST) // TIP: host is optional if you use https://us.i.posthog.com

docs render the TIP nicely

Copy link
Member

@marandaneto marandaneto Oct 26, 2024

Choose a reason for hiding this comment

The 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

Copy link
Contributor Author

Choose a reason for hiding this comment

The 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()
}
}
}
```
18 changes: 18 additions & 0 deletions contents/docs/integrate/_snippets/code-variants/ios-uikit.mdx
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
}
}
```
36 changes: 18 additions & 18 deletions contents/docs/integrate/_snippets/install-ios.mdx
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
import Tab from "components/Tab"
import IOSUIKit from "./code-variants/ios-uikit.mdx"
import IOSSwiftUI from "./code-variants/ios-swiftui.mdx"

PostHog is available through [CocoaPods](http://cocoapods.org) or you can add it as a Swift Package Manager based dependency.

### CocoaPods
Expand Down Expand Up @@ -28,21 +32,17 @@ and then as a dependency for the Package target utilizing PostHog:

### Configuration

```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
}
}
```
<Tab.Group tabs={['App Delegate/UIKit', 'SwiftUI Lifecycle']}>
<Tab.List>
<Tab>UIKit</Tab>
<Tab>SwiftUI</Tab>
</Tab.List>
<Tab.Panels>
<Tab.Panel>
{IOSUIKit}
</Tab.Panel>
<Tab.Panel>
{IOSSwiftUI}
</Tab.Panel>
</Tab.Panels>
</Tab.Group>
Loading