Legacy — WebView & Integration (old SDK)

Connecting WebView sessions, integrating Objective-C projects, recommendations, and support for the Sophonz iOS SDK.

WebView Session Connection

NOTE — Hybrid App (WebView) Guide

The Sophonz iOS SDK, Android SDK, and Browser SDK each collect data independently for their respective environments. When a web page is loaded through a WebView inside an iOS app, the WKWebViewConfiguration setup below is required to unify data into a single user session and collect WebView data.

To merge the Native session with the WebView session, first configure session sharing. The default is true (shared).

Sophonz.setSharedSession(session: Bool)
Initialization methodHandling
WKWebView initialized in codeSDK auto-injects scripts — no additional setup needed
WKWebView initialized via xibCall Sophonz.setWebviewConfiguration(userContentController:) manually
Using BridgeInitialize SophonzSDKBridge(webView:), call removeScriptMessageHandler() when leaving the screen

Option 1: WKWebView initialized in code

let config = WKWebViewConfiguration()
let view   = WKWebView(frame: .zero, configuration: config)

Option 2: WKWebView initialized via xib

let request = URLRequest(url: URL(string: "https://example.com/")!)
 
Sophonz.setWebviewConfiguration(
    userContentController: wkWebView.configuration.userContentController
)
wkWebView.load(request)

Option 3: Passing parameters to WebAgent via Bridge

// Pass the WebView instance when initializing Bridge in WebViewVC.
self.bridge = SophonzSDKBridge(webView: wkWebView)
 
// Always disconnect when WebViewVC is popped or disappears.
SophonzSDKBridge.removeScriptMessageHandler()

Objective-C Only Project Integration

If your project is entirely Objective-C, follow these steps to load the Swift runtime.

  1. Create a SwiftRuntimeLoader.swift file.
  2. When the "Would you like to create a bridging header?" dialog appears, select "Don't create".
  3. Add the following content to the newly created file.
import Foundation
@objc class SwiftRuntimeLoader: NSObject {}

This class ensures the Swift runtime loads correctly in an Objective-C project.

Adding SwiftUI Render Tracking

To track the lifecycle of SwiftUI views, add the following code to SceneDelegate.

import SophonzCore
 
func scene(_ scene: UIScene, willConnectTo session: UISceneSession,
           options connectionOptions: UIScene.ConnectionOptions) {
    if let windowScene = scene as? UIWindowScene {
        let window = UIWindow(windowScene: windowScene)
        window.rootViewController = UIHostingController(
            rootView: LandmarkList()
                .sophonzTrackLifecycle(viewName: nil)
                .environmentObject(UserData()))
        self.window = window
        window.makeKeyAndVisible()
    }
}

Recommendations

  • Initialize the SDK in AppDelegate's didFinishLaunchingWithOptions.
  • Use separate Collector URLs for development and production environments.
  • Set meaningful user IDs to improve session tracking accuracy.
  • Monitor for any SDK initialization errors separately.

Support

For further inquiries, please contact the Sophonz support team.