Skip to content

CapacitorBridge.presentVC does not work in Scenes based application #5951

@joeflateau

Description

@joeflateau

Hi! 👋

Firstly, thanks for your work on this project! 🙂

Today I used patch-package to patch @capacitor/ios@3.0.1 for the project I'm working on.

In a Scenes-based application (ie: any app that uses CarPlay) @capacitor/browser's Browser.open({ url }) does not work because they underlying CapacitorBridge.presentVC does not work.

Expected: call to Browser.open should open/animate-in a SFSafariViewController.

Actual: call to Browser.open appears to do nothing. I did some digging and what happens is the SFSafariViewController is presented on a UIWindow that is not in the view hierarchy.

Here is the diff that solved my problem:

diff --git a/node_modules/@capacitor/ios/Capacitor/Capacitor/CapacitorBridge.swift b/node_modules/@capacitor/ios/Capacitor/Capacitor/CapacitorBridge.swift
index 749ab1f..199dc83 100644
--- a/node_modules/@capacitor/ios/Capacitor/Capacitor/CapacitorBridge.swift
+++ b/node_modules/@capacitor/ios/Capacitor/Capacitor/CapacitorBridge.swift
@@ -658,7 +658,14 @@ internal class CapacitorBridge: NSObject, CAPBridgeProtocol {
         if viewControllerToPresent.modalPresentationStyle == .popover {
             self.viewController?.present(viewControllerToPresent, animated: flag, completion: completion)
         } else {
-            self.tmpWindow = UIWindow.init(frame: UIScreen.main.bounds)
+            if #available(iOS 13.0, *) {
+                if let windowScene = self.viewController?.view.window?.windowScene {
+                    self.tmpWindow = UIWindow.init(windowScene: windowScene)
+                }
+            }
+            if (self.tmpWindow == nil) {
+                self.tmpWindow = UIWindow.init(frame: UIScreen.main.bounds)
+            }
             self.tmpWindow?.rootViewController = TmpViewController.init()
             self.tmpWindow?.makeKeyAndVisible()
             self.tmpWindow?.rootViewController?.present(viewControllerToPresent, animated: flag, completion: completion)

This issue body was partially generated by patch-package.

Repro:
https://github.com/joeflateau/cap-scenes-browser

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions