mirror of
https://github.com/BillyOutlast/posthog.com.git
synced 2026-02-04 03:11:21 +01:00
Tutorial: How to set up iOS remote config (#10638)
This commit is contained in:
@@ -38,6 +38,7 @@ Got a question which isn't answered below? Head to [the community forum](/questi
|
||||
- [How to set up Flutter remote config](/tutorials/flutter-remote-config)
|
||||
- [How to set up Laravel feature flags](/tutorials/laravel-feature-flags)
|
||||
- [How to set up iOS feature flags](/tutorials/ios-feature-flags)
|
||||
- [How to set up iOS remote config](/tutorials/ios-remote-config)
|
||||
- [How to set up Next.js feature flags](/tutorials/nextjs-analytics#setting-up-and-using-feature-flags)
|
||||
- [How to set up Next.js app router feature flags](/tutorials/nextjs-app-directory-analytics#setting-up-feature-flags)
|
||||
- [How to set up Node.js (Express) feature flags](/tutorials/node-express-analytics#setting-up-feature-flags)
|
||||
|
||||
@@ -133,6 +133,8 @@ You can find more comparisons on [our feature flag comparisons page](/feature-fl
|
||||
|
||||
## Further reading
|
||||
|
||||
> **Want to get started with remote configs?** We wrote tutorials on how to set them up for [iOS](/tutorials/ios-remote-config), [Android](/tutorials/android-remote-config), [React Native](/tutorials/react-native-remote-config), and even [Flutter](/tutorials/flutter-remote-config).
|
||||
|
||||
- [Why use feature flags? Benefits, types and use cases, explained](/blog/feature-flag-benefits-use-cases)
|
||||
- [Feature flag best practices and tips (with examples)](/blog/feature-flag-best-practices)
|
||||
- [How to do a canary release with feature flags in PostHog](/tutorials/canary-release)
|
||||
|
||||
@@ -183,5 +183,5 @@ That's it! When you restart your app and click the button, you should see the gr
|
||||
- [How to set up iOS session replay](/tutorials/ios-session-replay)
|
||||
- [How to run A/B tests in iOS](/tutorials/ios-ab-tests)
|
||||
- [How to set up analytics in iOS](/tutorials/ios-analytics)
|
||||
|
||||
- [How to set up remote config in iOS](/tutorials/ios-remote-config)
|
||||
<NewsletterForm />
|
||||
146
contents/tutorials/ios-remote-config.md
Normal file
146
contents/tutorials/ios-remote-config.md
Normal file
@@ -0,0 +1,146 @@
|
||||
---
|
||||
title: How to set up iOS remote config
|
||||
date: 2025-02-11
|
||||
author:
|
||||
- ian-vanagas
|
||||
tags:
|
||||
- feature flags
|
||||
---
|
||||
|
||||
[Remote config](/docs/feature-flags/remote-config) enables you to update your iOS app's settings and behavior instantly without deploying new code or waiting for App Store approval. This helps you control features access on the fly and disable them instantly if needed.
|
||||
|
||||
This tutorial shows you how to set up remote config in your iOS app using PostHog. We'll create a basic iOS app using SwiftUI that displays a message controlled by remote config.
|
||||
|
||||
## 1. Creating a new iOS project
|
||||
|
||||
First, open Xcode and create a new project:
|
||||
|
||||
1. Select **File > New > Project**
|
||||
2. Choose **iOS** as the platform and **App** as the template
|
||||
3. Name your project (e.g., `PostHogRemoteConfig`)
|
||||
4. Select **SwiftUI** as the interface
|
||||
5. Leave the other settings as default and click **Next**
|
||||
6. Choose a location to save your project and click **Create**
|
||||
|
||||
## 2. Setting up PostHog
|
||||
|
||||
Next, we'll add [PostHog’s iOS SDK](/docs/libraries/ios) as a dependency. You can use either Swift Package Manager or CocoaPods. We'll use Swift Package Manager for this tutorial:
|
||||
|
||||
1. In Xcode, select **File > Add Package Dependencies**
|
||||
2. Enter `https://github.com/PostHog/posthog-ios.git` in the search field
|
||||
3. Click **Add Package**
|
||||
|
||||

|
||||
|
||||
Now, in `PostHogRemoteConfigApp.swift`, initialize PostHog with your project API key and host (both of which you can get in [your project settings](https://us.posthog.com/settings/project)).
|
||||
|
||||
```swift
|
||||
import SwiftUI
|
||||
import PostHog
|
||||
|
||||
@main
|
||||
struct PostHogRemoteConfigApp: App {
|
||||
init() {
|
||||
let config = PostHogConfig(
|
||||
apiKey: "<ph_project_api_key>",
|
||||
host: "<ph_client_api_host>"
|
||||
)
|
||||
PostHogSDK.shared.setup(config)
|
||||
}
|
||||
|
||||
var body: some Scene {
|
||||
WindowGroup {
|
||||
ContentView()
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
```
|
||||
|
||||
With this, you can build and run your app. Once launched, you should see events autocaptured into PostHog.
|
||||
|
||||
<ProductScreenshot
|
||||
imageLight="https://res.cloudinary.com/dmukukwp6/image/upload/Clean_Shot_2025_02_11_at_10_46_17_2x_13073f4f5e.png"
|
||||
imageDark="https://res.cloudinary.com/dmukukwp6/image/upload/Clean_Shot_2025_02_11_at_10_45_53_2x_82ad60a5b7.png"
|
||||
alt="Events autocaptured into PostHog"
|
||||
classes="rounded"
|
||||
/>
|
||||
|
||||
## 3. Creating our remote config
|
||||
|
||||
Now, let's set up the remote config flag in PostHog to control our welcome message:
|
||||
|
||||
1. Go to the [feature flags tab](https://us.posthog.com/feature_flags) in PostHog
|
||||
2. Click **New feature flag**
|
||||
3. Set the key as `welcome-message`
|
||||
4. Under **Served value**, select **Remote config (single payload)**
|
||||
5. Set the payload to a string: `"Welcome to our awesome iOS app!"`
|
||||
6. Click **Save**
|
||||
|
||||
<ProductScreenshot
|
||||
imageLight="https://res.cloudinary.com/dmukukwp6/image/upload/Clean_Shot_2025_02_11_at_10_36_23_2x_c7ec443c95.png"
|
||||
imageDark="https://res.cloudinary.com/dmukukwp6/image/upload/Clean_Shot_2025_02_11_at_10_36_39_2x_63251770aa.png"
|
||||
alt="Remote config flag created in PostHog"
|
||||
classes="rounded"
|
||||
/>
|
||||
|
||||
## 4. Setting up the app UI
|
||||
|
||||
To show that message, let's create a simple UI in `ContentView.swift` that displays it. This requires:
|
||||
|
||||
- A basic view with a text element to display our message
|
||||
- A `getFeatureFlagPayload()` call to fetch our remote config value
|
||||
- A default message fall back if the remote config isn't available
|
||||
- A refresh button to reload the message if we change the remote config
|
||||
|
||||
All together this looks like this:
|
||||
|
||||
```swift
|
||||
import SwiftUI
|
||||
import PostHog
|
||||
|
||||
struct ContentView: View {
|
||||
@State private var welcomeMessage: String = "Loading..."
|
||||
|
||||
var body: some View {
|
||||
VStack(spacing: 20) {
|
||||
Text(welcomeMessage)
|
||||
.font(.title)
|
||||
.multilineTextAlignment(.center)
|
||||
.padding()
|
||||
|
||||
Button("Refresh Message") {
|
||||
loadWelcomeMessage()
|
||||
}
|
||||
.padding()
|
||||
.background(Color.blue)
|
||||
.foregroundColor(.white)
|
||||
.cornerRadius(10)
|
||||
}
|
||||
.onAppear {
|
||||
loadWelcomeMessage()
|
||||
}
|
||||
}
|
||||
|
||||
private func loadWelcomeMessage() {
|
||||
if let message = PostHogSDK.shared.getFeatureFlagPayload("welcome-message") as? String {
|
||||
welcomeMessage = message
|
||||
} else {
|
||||
welcomeMessage = "Welcome to the app!"
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
```
|
||||
|
||||
To test this out, build and run your app again. You should see the welcome message from your remote config displayed in the center of the screen.
|
||||
|
||||

|
||||
|
||||
You can also go back into PostHog, update your remote config, and then press refresh in-app to see the updated message.
|
||||
|
||||
## Further reading
|
||||
|
||||
- [How to set up analytics in iOS](/tutorials/ios-analytics)
|
||||
- [How to run A/B tests in iOS](/tutorials/ios-ab-tests)
|
||||
- [Feature flags vs configuration: Which should you choose?](/product-engineers/feature-flags-vs-configuration)
|
||||
Reference in New Issue
Block a user