ionic-team / capacitor

Build cross-platform Native Progressive Web Apps for iOS, Android, and the Web ⚡️
https://capacitorjs.com
MIT License
12.06k stars 1k forks source link

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

Open joeflateau opened 2 years ago

joeflateau commented 2 years ago

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

Ionitron commented 2 years ago

This issue may need more information before it can be addressed. In particular, it will need a reliable Code Reproduction that demonstrates the issue.

Please see the Contributing Guide for how to create a Code Reproduction.

Thanks! Ionitron 💙

joeflateau commented 2 years ago

added a repro: https://github.com/joeflateau/cap-scenes-browser