Initializing the PageSense iOS SDK

Initializing the PageSense iOS SDK

Before you start  

PageSenseClient is the core interface for everything — activating experiments, retrieving variations, tracking goals. None of that works until the SDK is initialized.
Initialization fetches your project configuration from the PageSense server and caches it locally on the device. This includes:
  • All FullStack experiments configured in your project.
  • The current status of each experiment.
  • Traffic allocation and audience targeting rules.

Method  

  1. swift
  2. import PageSenseFramework
  3. PageSense.shared.createNewPageSenseClient(
  4.     sdkAccountIDForDC: String,
  5.     sdkKey: String,
  6.     projectname: String,
  7.     sdkOptions: PageSenseSDKOptions
  8. ) { client in
  9.     if client != nil {
  10.         print("PageSense SDK initialised successfully")
  11.     } else {
  12.         print("Initialization failed")
  13.     }
  14. }
  15.  

Parameters  

Parameter
Type
Description
sdkAccountIDForDC
String
Your PageSense account identifier.
sdkKey
String
SDK key for your environment. Find it in SDK Settings on the FullStack project configuration page.
projectname
String
The PageSense project containing your experiments.
sdkOptions
PageSenseSDKOptions
Optional. Configure logging, polling interval, and other SDK behaviour.

What happens during initialization  

When createNewPageSenseClient() is called:
  1. The SDK sends a network request to fetch project settings from PageSense.
  2. Settings are parsed and cached locally on the device.
  3. A PageSenseClient instance is created and returned via the completion handler.
From that point, the client is ready to evaluate experiments and track events.
If initialization fails — wrong credentials, no network, missing project config — the returned client will be nil.

Usage Example  

  1. swift
  2. import PageSenseFramework
  3. // Declare the client as a class variable
  4. var pageSenseClient: PageSenseClient?
  5. // Create SDK options
  6. let pageSenseSDKOptions = PageSenseSDKOptions()
  7. // Initialize the SDK
  8. PageSense.shared.createNewPageSenseClient(
  9.     sdkAccountIDForDC: "YOUR_ACCOUNT_ID",
  10.     sdkKey: "YOUR_SDK_KEY",
  11.     projectname: "YOUR_PROJECT_NAME",
  12.     sdkOptions: pageSenseSDKOptions
  13. ) { client in
  14.     guard let client = client else {
  15.         print("PageSenseClient initialization failed")
  16.         return
  17.     }
  18.     self.pageSenseClient = client
  19.     print("PageSenseClient initialised successfully")
  20. }

Eager vs Lazy Initialization  

Eager (recommended)
Lazy
Eager (recommended)
Initialize at app launch — before any user interaction. This ensures experiment configurations are ready the moment your first screen loads. Users are bucketed consistently from the start.
Use this when experiments affect initial screens or core app behaviour.

Lazy
Initialize only when a specific feature or screen needs it. Reduces startup overhead, but risks a delay in fetching configurations.
Use this when experiments are limited to a specific section of your app and you want to defer the network call.
For most apps, eager initialization is the right call. Initializing early means no race conditions between SDK readiness and experiment evaluation.


Thread Safety  

The SDK is safe to use in a multi-threaded environment, but keep these in mind:
  • Don't run heavy SDK operations on the main thread.
  • Use the completion handler to receive the initialized client — it won't block UI rendering.
  • Store and reuse the PageSenseClient instance across your app rather than reinitializing.
  • Always update the UI based on variation results on the main thread.
  1. swift
  2. DispatchQueue.main.async {
  3.     // Apply UI changes based on assigned variation
  4. }
A few things to keep in mind  
  • createNewPageSenseClient() is asynchronous — handle the result inside the completion handler.
  • Initialize in AppDelegate, SceneDelegate, or your SwiftUI App initializer.
  • Always guard against a nil client — don't assume initialization succeeded.



We’ve designed this documentation to guide you every step of the way. If you need further assistance or have any questions, don’t hesitate to contact us at support@zohopagesense.com - we’re always here to help!