From ec7fb2684c09d37dd2a1d65ef28b6082d3ddc7e6 Mon Sep 17 00:00:00 2001 From: resp76 <202176107+resp76@users.noreply.github.com> Date: Fri, 20 Nov 2026 07:47:19 -0500 Subject: [PATCH] fix(cli): keep a storyboard CAPBridgeViewController subclass when migrating to SwiftUI Co-Authored-By: Claude Opus 5.5 --- cli/src/tasks/migrate-swiftui.ts | 42 ++++++++++++++++++- cli/test/fixtures/pre-swiftui-main.storyboard | 19 +++++++++ cli/test/migrate-swiftui-storyboard.spec.ts | 41 ++++++++++++++++++ 3 files changed, 100 insertions(+), 2 deletions(-) create mode 100644 cli/test/fixtures/pre-swiftui-main.storyboard create mode 100644 cli/test/migrate-swiftui-storyboard.spec.ts diff --git a/cli/src/tasks/migrate-swiftui.ts b/cli/src/tasks/migrate-swiftui.ts index 604a3d77a1..1ecf18d5c9 100644 --- a/cli/src/tasks/migrate-swiftui.ts +++ b/cli/src/tasks/migrate-swiftui.ts @@ -134,13 +134,29 @@ export async function migrateToSwiftUI(config: Config): Promise { }); await runTask('Writing App.swift, CapacitorView.swift and SceneDelegate.swift.', async () => { + const storyboardPath = join(config.ios.nativeTargetDirAbs, 'Base.lproj', 'Main.storyboard'); + const bridgeClass = existsSync(storyboardPath) + ? getStoryboardBridgeSubclass(readFileSync(storyboardPath, 'utf-8')) + : null; + const capacitorView = bridgeClass ? useBridgeSubclass(assets.capacitorView, bridgeClass) : assets.capacitorView; const files: [string, string][] = [ ['App.swift', assets.app], - ['CapacitorView.swift', assets.capacitorView], + ['CapacitorView.swift', capacitorView], ]; for (const [name, contents] of files) { - if (!writeAppTargetFile(config, name, contents)) { + if (writeAppTargetFile(config, name, contents)) { + if (name === 'CapacitorView.swift' && bridgeClass) { + logger.info( + `CapacitorView.swift uses your ${bridgeClass} from Main.storyboard as the bridge view controller.`, + ); + } + } else { logger.warn(`${name} already exists, skipping.`); + if (name === 'CapacitorView.swift' && bridgeClass) { + logger.warn( + `Main.storyboard set ${bridgeClass} as the bridge view controller: make sure CapacitorView.swift creates it.`, + ); + } } } @@ -217,6 +233,26 @@ function writeSceneDelegate(path: string, contents: string): 'written' | 'replac return 'replaced'; } +/** + * Main.storyboard is about to be removed, so a CAPBridgeViewController subclass set on its initial + * view controller (the documented way to register local plugins) would be silently dropped. + * Returns that subclass, or null when the storyboard uses CAPBridgeViewController itself. + */ +function getStoryboardBridgeSubclass(storyboard: string): string | null { + const initialId = storyboard.match(/\binitialViewController="([^"]+)"/)?.[1]; + if (!initialId) return null; + const tag = [...storyboard.matchAll(/]*>/g)] + .map((m) => m[0]) + .find((t) => t.includes(` id="${initialId}"`)); + const customClass = tag?.match(/\bcustomClass="([A-Za-z_][A-Za-z0-9_]*)"/)?.[1]; + return customClass && customClass !== 'CAPBridgeViewController' ? customClass : null; +} + +/** Point the template CapacitorView at the app's own bridge subclass. */ +function useBridgeSubclass(capacitorView: string, className: string): string { + return capacitorView.replace(/\bCAPBridgeViewController\b/g, className); +} + /** * Drop the storyboard the SwiftUI App struct replaces. * @@ -705,6 +741,7 @@ export const __testables = { classify, describeSignals, ensureSupportedInterfaceOrientations, + getStoryboardBridgeSubclass, hasCustomDelegateBody, hasCustomWindowSetup, isStockAppDelegate, @@ -712,5 +749,6 @@ export const __testables = { removeLeftoverStoryboard, rewriteAppDelegateForAdaptor, scanAndWarn, + useBridgeSubclass, writeSceneDelegate, }; diff --git a/cli/test/fixtures/pre-swiftui-main.storyboard b/cli/test/fixtures/pre-swiftui-main.storyboard new file mode 100644 index 0000000000..b44df7be8f --- /dev/null +++ b/cli/test/fixtures/pre-swiftui-main.storyboard @@ -0,0 +1,19 @@ + + + + + + + + + + + + + + + + + + + diff --git a/cli/test/migrate-swiftui-storyboard.spec.ts b/cli/test/migrate-swiftui-storyboard.spec.ts new file mode 100644 index 0000000000..e766886bb4 --- /dev/null +++ b/cli/test/migrate-swiftui-storyboard.spec.ts @@ -0,0 +1,41 @@ +import { readFileSync } from 'fs-extra'; +import { resolve } from 'path'; + +import { __testables } from '../src/tasks/migrate-swiftui'; + +const { getStoryboardBridgeSubclass, useBridgeSubclass } = __testables; + +// The 8.x template's Main.storyboard, which cap migrate removes. +const STOCK_STORYBOARD = readFileSync(resolve(__dirname, 'fixtures/pre-swiftui-main.storyboard'), 'utf-8'); +const TEMPLATE_CAPACITOR_VIEW = readFileSync( + resolve(__dirname, '../../ios-spm-template/App/App/CapacitorView.swift'), + 'utf-8', +); + +describe('getStoryboardBridgeSubclass', () => { + it('returns null for the stock 8.x storyboard', () => { + expect(getStoryboardBridgeSubclass(STOCK_STORYBOARD)).toBeNull(); + }); + + it('returns the custom class of the initial view controller', () => { + const custom = STOCK_STORYBOARD.replace( + 'customClass="CAPBridgeViewController" customModule="Capacitor"', + 'customClass="MyViewController" customModule="App" customModuleProvider="target"', + ); + expect(getStoryboardBridgeSubclass(custom)).toBe('MyViewController'); + }); + + it('returns null when there is no initial view controller', () => { + expect(getStoryboardBridgeSubclass('')).toBeNull(); + }); +}); + +describe('useBridgeSubclass', () => { + it('makes the template CapacitorView create and type the subclass', () => { + const result = useBridgeSubclass(TEMPLATE_CAPACITOR_VIEW, 'MyViewController'); + expect(result).toContain('-> MyViewController {'); + expect(result).toContain(' MyViewController()'); + expect(result).toContain('_ vc: MyViewController,'); + expect(result).not.toContain('CAPBridgeViewController'); + }); +});