[Bug]: iOS UIScene template starts a second web app instance when an external display or AirPlay mirroring connects

オープン
#8,648 コメント 0 件 リアクション 3 件 担当者 1 名 GitHub で見る

メンテナーはふだん 2 日以内に返信

@theproducer がすでに取り組んでいます。

2026年10月9日 から。

評価

この issue はまだ評価されていません。

説明

platform: ios
Capacitor Version
💊   Capacitor Doctor  💊

Latest Dependencies:

  @capacitor/cli: 8.5.3
  @capacitor/core: 8.5.3
  @capacitor/android: 8.5.3
  @capacitor/ios: 8.5.3

Installed Dependencies:

  @capacitor/cli: 8.5.2
  @capacitor/android: 8.5.2
  @capacitor/core: 8.5.2
  @capacitor/ios: 8.5.2

[success] iOS looking great! 👌
Other API Details
npm --version: 11.4.2
node --version: v22.16.0
pod --version: 1.16.2
Xcode 26.3 (17C529)
Plugins mentioned below: @capacitor-community/sqlite 8.1.1, @capacitor/screen-orientation 8.0.2
Platforms Affected
  • iOS
  • Android
  • Web
Current Behavior

Since the 8.5 iOS template adopted UIScene, an app made from it starts a second, independent copy of the web app when an external display is connected or AirPlay screen mirroring starts.

Cause, in ios-pods-template/App/App/ (the SPM template has the same code):

  • AppDelegate.application(_:configurationForConnecting:options:) returns a configuration with delegateClass = SceneDelegate.self for every session role.
  • SceneDelegate.scene(_:willConnectTo:options:) creates a UIWindow with a new CAPBridgeViewController() for every UIWindowScene.

When an external display connects, iOS calls configurationForConnecting with the role UIWindowSceneSessionRoleExternalDisplayNonInteractive, even though UIApplicationSupportsMultipleScenes is false. Apple DTS confirms this and says the app should not create a window for that scene unless it wants to stop mirroring: https://developer.apple.com/forums/thread/815376. (Info.plist declares the SceneDelegate only for UIWindowSceneSessionRoleApplication, but the AppDelegate method returns it for all roles.)

Result: a second CAPBridgeViewController with its own WKWebView, bridge and plugin instances, running in parallel to the main one:

  • The external display shows this separate copy of the app (it stays on the start page and does not follow the device) instead of mirroring the device screen.
  • Both instances share local storage. With @capacitor-community/sqlite this caused database is locked errors in production on iPads.
  • Plugins initialise twice (e.g. in-app purchase listeners).
Expected Behavior

Only the windowApplication scene gets a CAPBridgeViewController. Connecting an external display or starting AirPlay mirroring does not start a second web view, and iOS mirrors the device screen as usual.

Project Reproduction

any app created from the Capacitor 8.5 iOS template reproduces it

Additional Information

No separate repository needed: any app created from the Capacitor 8.5 iOS template reproduces it.

  1. Run the app in the iOS Simulator (we used iPad Air 11-inch (M3), iOS 18.5; same with iOS 26.2).
  2. Simulator menu I/O → External Displays → any resolution.
  3. The Xcode/simctl console shows a second ⚡️ Loading app at capacitor://localhost..., and Safari → Develop lists two inspectable pages for the app.

In our run iOS connected the external display scene, disconnected it and connected it again, which gave three web views.

On a real device (iPad, iPadOS 18.7.8, debug build from Xcode, AirPlay screen mirroring to a Mac):

  1. Start the app, then start screen mirroring from Control Center.
  2. The Xcode console shows another ⚡️ Loading app at capacitor://localhost... each time mirroring starts.
  3. The Mac shows the app's start page and does not follow what happens on the iPad.

Proposed fix (what we use now), in AppDelegate.swift:

func application(_ application: UIApplication,
                 configurationForConnecting connectingSceneSession: UISceneSession,
                 options: UIScene.ConnectionOptions) -> UISceneConfiguration {
    let config = UISceneConfiguration(name: "Default Configuration", sessionRole: connectingSceneSession.role)
    if connectingSceneSession.role == .windowApplication {
        config.delegateClass = SceneDelegate.self
    }
    return config
}

Verified with the fix:

  • Simulator, iOS 18.5 and 26.2: only one web app instance with an external display attached, and also on a cold start with the display already attached. The Simulator shows a black external display for every app, because it does not mirror.
  • Real iPad, iPadOS 18.7.8, AirPlay to a Mac:
    • Starting mirroring several times, and a cold start while mirroring, always gave exactly one Loading app at.
    • The Mac mirrors the iPad screen and follows it.

An alternative is a role guard at the top of SceneDelegate.scene(_:willConnectTo:options:): guard session.role == .windowApplication else { return }.

Where the same code lives:

  • Templates on main (checked on 2026-10-08, identical to 8.5.2 and 8.5.3): ios-pods-template/App/App/AppDelegate.swift, ios-spm-template/App/App/AppDelegate.swift, and the SceneDelegate.swift next to them.
  • The UIScene migrator (#8544) copies SceneDelegate.swift and the configurationForConnecting method from the template archive, so a template fix reaches newly migrated projects. It skips projects that already contain UISceneConfiguration(name:, so apps that already migrated keep the old code and need a note in the update guide.
  • The 8.5 update guide shows the same snippet (step 3 in docs/main/updating/8-5.md of capacitor-docs): https://capacitorjs.com/docs/updating/8-5

Makes it worse: @capacitor/screen-orientation 8.0.2 holds CAPBridgeViewController strongly (ScreenOrientation.capViewController), so a bridge is never released and the web views of disconnected external display scenes keep running after the display is removed. Already reported in ionic-team/capacitor-plugins#2462.

See also: the same bug class in SDL: libsdl-org/SDL#16465.

主要言語
TypeScript
スター
17.1k
フォーク
1.3k
平均マージ
2日 1時間
マージ済み PR(30日)
32

環境構築

はじめの一歩

  1. issue を最後まで読み、次にプロジェクトのコントリビューションガイドを読みます。
  2. 着手することを issue にコメントします — 二人が同じ作業をするのを防げます。
  3. リポジトリをフォークし、ブランチを切って変更します。
  4. issue 番号を参照したプルリクエストを送ります。

ionic-team/capacitor のほかの issue

ionic-team/capacitor の issue をすべて見る

似ている issue

TypeScript の issue をもっと見る

新しい issue をメールで受け取る

初心者向けの GitHub issue を短くまとめたダイジェスト。