Skip to content
Closed
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
2 changes: 2 additions & 0 deletions apps/basic-integration/swift/hackers-ios/.env.example
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
POSTHOG_PROJECT_TOKEN=your_posthog_project_token
POSTHOG_HOST=https://your-posthog-host
36 changes: 36 additions & 0 deletions apps/basic-integration/swift/hackers-ios/App/AppDelegate.swift
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,16 @@
//

import Data
import PostHog
import Shared
import UIKit

class AppDelegate: NSObject, UIApplicationDelegate {
func application(_: UIApplication,
didFinishLaunchingWithOptions _: [UIApplication.LaunchOptionsKey: Any]?) -> Bool
{
configurePostHog()

// Configure a modest shared URL cache to limit on-disk growth from image/HTTP caching
// This affects system components like AsyncImage that use URLSession.shared
let memoryCapacity = 64 * 1024 * 1024 // 64 MB
Expand All @@ -36,4 +39,37 @@ class AppDelegate: NSObject, UIApplicationDelegate {

return true
}

private func configurePostHog() {
guard let projectToken = Bundle.main.object(forInfoDictionaryKey: "POSTHOG_PROJECT_TOKEN") as? String,
!projectToken.isEmpty,
!projectToken.hasPrefix("$(")
else {
#if DEBUG
assertionFailure("POSTHOG_PROJECT_TOKEN variable required by PostHog is missing or un-configured, this causes events to be silently missed. This error stops appearing once POSTHOG_PROJECT_TOKEN is configured")
#endif
return
}

guard let host = Bundle.main.object(forInfoDictionaryKey: "POSTHOG_HOST") as? String,
!host.isEmpty,
!host.hasPrefix("$(")
else {
#if DEBUG
assertionFailure("POSTHOG_HOST variable required by PostHog is missing or un-configured, this causes events to be silently missed. This error stops appearing once POSTHOG_HOST is configured")
#endif
return
}

let config = PostHogConfig(projectToken: projectToken, host: host)
config.errorTrackingConfig.autoCapture = true
config.logs.serviceName = Bundle.main.bundleIdentifier ?? "Hackers"
#if DEBUG
config.logs.environment = "debug"
#else
config.logs.environment = "production"
#endif
PostHogSDK.shared.setup(config)
PostHogSDK.shared.logger?.info("PostHog log capture initialized")
}
}
55 changes: 34 additions & 21 deletions apps/basic-integration/swift/hackers-ios/App/ContentView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import Comments
import DesignSystem
import Domain
import Feed
import PostHog
import Settings
import Shared
import SwiftUI
Expand Down Expand Up @@ -79,12 +80,8 @@ struct MainContentView: View {
viewModel: settingsViewModel,
isAuthenticated: sessionService.authenticationState == .authenticated,
currentUsername: sessionService.username,
onLogin: { username, password in
_ = try await sessionService.authenticate(username: username, password: password)
},
onLogout: {
sessionService.unauthenticate()
},
onLogin: authenticate,
onLogout: logout,
onShowOnboarding: {
showOnboarding = true
}
Expand All @@ -101,12 +98,8 @@ struct MainContentView: View {
LoginView(
isAuthenticated: sessionService.authenticationState == .authenticated,
currentUsername: sessionService.username,
onLogin: { username, password in
_ = try await sessionService.authenticate(username: username, password: password)
},
onLogout: {
sessionService.unauthenticate()
},
onLogin: authenticate,
onLogout: logout,
textSize: settingsViewModel.textSize
)
.textScaling(for: settingsViewModel.textSize)
Expand All @@ -117,12 +110,8 @@ struct MainContentView: View {
viewModel: settingsViewModel,
isAuthenticated: sessionService.authenticationState == .authenticated,
currentUsername: sessionService.username,
onLogin: { username, password in
_ = try await sessionService.authenticate(username: username, password: password)
},
onLogout: {
sessionService.unauthenticate()
},
onLogin: authenticate,
onLogout: logout,
onShowOnboarding: {
showOnboarding = true
}
Expand All @@ -132,9 +121,7 @@ struct MainContentView: View {
}
.sheet(isPresented: $showOnboarding) {
onboardingCoordinator
.makeOnboardingView {
showOnboarding = false
}
.makeOnboardingView(onDismiss: onboardingDismissed)
.textScaling(for: settingsViewModel.textSize)
.toastOverlay(toastPresenter)
}
Expand All @@ -143,6 +130,32 @@ struct MainContentView: View {
showOnboarding = true
}
}
.onReceive(NotificationCenter.default.publisher(for: .bookmarksDidChange)) { notification in
guard let isBookmarked = notification.userInfo?["isBookmarked"] as? Bool else { return }
PostHogSDK.shared.capture(isBookmarked ? "bookmark_added" : "bookmark_removed")
PostHogSDK.shared.logger?.info(
"Bookmark state changed",
attributes: ["is_bookmarked": isBookmarked]
)
}
}

private func authenticate(username: String, password: String) async throws {
_ = try await sessionService.authenticate(username: username, password: password)
PostHogSDK.shared.capture("login_succeeded")
PostHogSDK.shared.logger?.info("Authentication completed")
}

private func logout() {
PostHogSDK.shared.capture("logout_requested")
PostHogSDK.shared.logger?.info("Logout requested")
sessionService.unauthenticate()
}

private func onboardingDismissed() {
PostHogSDK.shared.capture("onboarding_dismissed")
PostHogSDK.shared.logger?.info("Onboarding dismissed")
showOnboarding = false
}

private var isPresentingModal: Bool {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,10 @@
<false/>
<key>LSRequiresIPhoneOS</key>
<true/>
<key>POSTHOG_HOST</key>
<string>$(POSTHOG_HOST)</string>
<key>POSTHOG_PROJECT_TOKEN</key>
<string>$(POSTHOG_PROJECT_TOKEN)</string>
<key>NSAppTransportSecurity</key>
<dict>
<key>NSAllowsArbitraryLoads</key>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
24F39F8B18AFB1150055F8DC /* CoreGraphics.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = 24F39F8A18AFB1150055F8DC /* CoreGraphics.framework */; };
24F39F8D18AFB1150055F8DC /* UIKit.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = 24F39F8C18AFB1150055F8DC /* UIKit.framework */; };
DA78360FD99A4A1D9C1E759C /* Authentication in Frameworks */ = {isa = PBXBuildFile; productRef = B2F5BEA03AC34B9DB788D8E2 /* Authentication */; };
F0A1B2C3D4E5F60718293A4B /* PostHog in Frameworks */ = {isa = PBXBuildFile; productRef = F0A1B2C3D4E5F60718293A4A /* PostHog */; };
/* End PBXBuildFile section */

/* Begin PBXContainerItemProxy section */
Expand Down Expand Up @@ -102,6 +103,7 @@
2406B38728AD083500CB396B /* SwiftSoup in Frameworks */,
241E7B412E511BC20013EEA1 /* Data in Frameworks */,
248256FB2E59D2540068E91E /* Networking in Frameworks */,
F0A1B2C3D4E5F60718293A4B /* PostHog in Frameworks */,
);
runOnlyForDeploymentPostprocessing = 0;
};
Expand Down Expand Up @@ -192,6 +194,7 @@
248256FD2E59D2660068E91E /* Feed */,
2482576D2E5B38190068E91E /* Onboarding */,
B2F5BEA03AC34B9DB788D8E2 /* Authentication */,
F0A1B2C3D4E5F60718293A4A /* PostHog */,
);
productName = Hackers2;
productReference = 24F39F8518AFB1150055F8DC /* Hackers.app */;
Expand Down Expand Up @@ -237,6 +240,7 @@
248256FC2E59D2660068E91E /* XCLocalSwiftPackageReference "Features/Feed" */,
2482576C2E5B38190068E91E /* XCLocalSwiftPackageReference "Features/Onboarding" */,
52A3FC99203E47A9B5690643 /* XCLocalSwiftPackageReference "Features/Authentication" */,
F0A1B2C3D4E5F60718293A49 /* XCRemoteSwiftPackageReference "PostHog" */,
);
productRefGroup = 24F39F8618AFB1150055F8DC /* Products */;
projectDirPath = "";
Expand Down Expand Up @@ -510,6 +514,8 @@
"@executable_path/Frameworks",
);
MARKETING_VERSION = 5.2.3;
POSTHOG_HOST = https://us.i.posthog.com;
POSTHOG_PROJECT_TOKEN = phx_API_KEY_IS_HARDCODED;
PRODUCT_BUNDLE_IDENTIFIER = com.weiranzhang.Hackers;
PRODUCT_NAME = Hackers;
PROVISIONING_PROFILE_SPECIFIER = "";
Expand Down Expand Up @@ -541,6 +547,8 @@
"@executable_path/Frameworks",
);
MARKETING_VERSION = 5.2.3;
POSTHOG_HOST = https://us.i.posthog.com;
POSTHOG_PROJECT_TOKEN = phx_API_KEY_IS_HARDCODED;
PRODUCT_BUNDLE_IDENTIFIER = com.weiranzhang.Hackers;
PRODUCT_NAME = Hackers;
PROVISIONING_PROFILE_SPECIFIER = "";
Expand Down Expand Up @@ -631,6 +639,14 @@
minimumVersion = 2.0.0;
};
};
F0A1B2C3D4E5F60718293A49 /* XCRemoteSwiftPackageReference "PostHog" */ = {
isa = XCRemoteSwiftPackageReference;
repositoryURL = "https://github.com/PostHog/posthog-ios.git";
requirement = {
kind = upToNextMajorVersion;
minimumVersion = 3.92.7;
};
};
/* End XCRemoteSwiftPackageReference section */

/* Begin XCSwiftPackageProductDependency section */
Expand Down Expand Up @@ -671,6 +687,11 @@
isa = XCSwiftPackageProductDependency;
productName = Authentication;
};
F0A1B2C3D4E5F60718293A4A /* PostHog */ = {
isa = XCSwiftPackageProductDependency;
package = F0A1B2C3D4E5F60718293A49 /* XCRemoteSwiftPackageReference "PostHog" */;
productName = PostHog;
};
/* End XCSwiftPackageProductDependency section */
};
rootObject = 24F39F7D18AFB1150055F8DC /* Project object */;
Expand Down

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading