Skip to content

fix(ios): align user agent with analytics-swift - #222

Merged
MichaelGHSeg merged 2 commits into
segmentio:mainfrom
zachdean:webkit-start
Oct 16, 2026
Merged

MichaelGHSeg merged 2 commits into
segmentio:mainfrom
zachdean:webkit-start

Conversation

@zachdean

Copy link
Copy Markdown
Contributor

Fixes #221

What

This aligns the plugin's iOS and macOS user agent with analytics-swift's UserAgent.swift, which replaced the same WKWebView lookup in analytics-swift#341 (released in 1.5.12).

  • iOS builds the string from UIDevice.current.model and ProcessInfo.operatingSystemVersion, using the same format as analytics-swift. It's computed once in a static let, so it's safe to read from any thread.
  • macOS returns the same hard-coded string as analytics-swift.
  • import WebKit and the DispatchQueue.main.sync fallback are removed.
  • AppleUtils.swift gets the same change in both the ios/ and macos/ source trees.

Why

The first WKWebView in a process starts WebKit. In a profile build of our app on an iPhone 12 (iOS 26.3), this blocked the main thread for about 290ms at launch. Generating the string instead brought getContext down to ~0.5ms. Since Flutter 3.29 (iOS) and 3.35 (macOS), Dart runs on the main thread, so the block stalls Flutter frames directly. analytics-swift made this change after the same call caused hangs and crashes (#229, #353).

Behaviour change

The output now matches analytics-swift byte for byte. Compared with the WebKit value the plugin sent before:

Before (WKWebView) After (analytics-swift)
iPhone, iOS 26.5 iPhone; CPU iPhone OS 18_7 iPhone; CPU iPhone OS 26_5
iPad, iOS 26.5 iPad; CPU OS 18_7 iPad; CPU iPad OS 26_5
iPhone, iOS 16.0 identical identical
macOS 26.5 identical identical

Testing

  • Ran an XCTest port of analytics-swift's UserAgentTests.swift against this AppleUtils.swift. It checks the format, caching across threads, and comparison with WKWebView. It passes on iPhone 17 Pro and iPad Pro 11" (iOS 26.5), iPhone 14 (iOS 16.0), and macOS 26.5. It isn't included because the repo has no native test target.
  • Built the example app against the local plugin for the iOS simulator and macOS.
  • No Dart changes.

zachdean and others added 2 commits October 13, 2026 09:18
getContext read the user agent by creating a WKWebView. The first web view
in a process has to start WebKit, which blocks the main thread at launch
(~290ms on an iPhone 12). Since Flutter 3.29 runs Dart on the platform
thread, that also stalls Flutter frames.

Build the string from the device model and OS version instead, as
analytics-swift does since segmentio/analytics-swift#341, compute it once,
and drop the WebKit import on iOS and macOS.
@MichaelGHSeg
MichaelGHSeg merged commit 317a303 into segmentio:main Oct 16, 2026
9 of 16 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

iOS/macOS: align user agent with analytics-swift instead of creating a WKWebView on the main thread

2 participants