From 7ccd7e0525f214d6c7131d9e2236b612ee9f0414 Mon Sep 17 00:00:00 2001 From: jcesarmobile Date: Thu, 2 Apr 2020 16:48:38 +0200 Subject: [PATCH 1/2] feat(ios): add presentVC and dismissVC methods to bridge --- ios/Capacitor/Capacitor/CAPBridge.swift | 16 ++++++++++++++++ ios/Capacitor/Capacitor/Plugins/Browser.swift | 4 ++-- ios/Capacitor/Capacitor/TmpViewController.swift | 8 ++++++++ 3 files changed, 26 insertions(+), 2 deletions(-) create mode 100644 ios/Capacitor/Capacitor/TmpViewController.swift diff --git a/ios/Capacitor/Capacitor/CAPBridge.swift b/ios/Capacitor/Capacitor/CAPBridge.swift index b0bdd4b361..4ec3ab3383 100644 --- a/ios/Capacitor/Capacitor/CAPBridge.swift +++ b/ios/Capacitor/Capacitor/CAPBridge.swift @@ -9,7 +9,10 @@ enum BridgeError: Error { @objc public class CAPBridge : NSObject { + let tmpWindow = UIWindow.init(frame: UIScreen.main.bounds) + let tmpVC = TmpViewController.init() @objc public static let statusBarTappedNotification = Notification(name: Notification.Name(rawValue: "statusBarTappedNotification")) + @objc public static let tmpVCAppeared = Notification(name: Notification.Name(rawValue: "tmpViewControllerAppeared")) public static var CAP_SITE = "https://capacitor.ionicframework.com/" public static var CAP_FILE_START = "/_capacitor_file_" public static let CAP_DEFAULT_SCHEME = "capacitor" @@ -63,6 +66,10 @@ enum BridgeError: Error { registerPlugins() setupCordovaCompatibility() bindObservers() + self.tmpWindow.rootViewController = tmpVC + NotificationCenter.default.addObserver(forName: CAPBridge.tmpVCAppeared.name, object: .none, queue: .none) { _ in + self.tmpWindow.isHidden = true + } } public func setStatusBarVisible(_ isStatusBarVisible: Bool) { @@ -589,5 +596,14 @@ enum BridgeError: Error { return localUrl! } + @objc public func presentVC(_ viewControllerToPresent: UIViewController, animated flag: Bool, completion: (() -> Void)? = nil) { + self.tmpWindow.makeKeyAndVisible() + self.tmpVC.present(viewControllerToPresent, animated: flag, completion: completion) + } + + @objc public func dismissVC(animated flag: Bool, completion: (() -> Void)? = nil) { + self.tmpVC.dismiss(animated: flag, completion: completion) + } + } diff --git a/ios/Capacitor/Capacitor/Plugins/Browser.swift b/ios/Capacitor/Capacitor/Plugins/Browser.swift index d9b0d98f26..a768de6d90 100644 --- a/ios/Capacitor/Capacitor/Plugins/Browser.swift +++ b/ios/Capacitor/Capacitor/Plugins/Browser.swift @@ -34,7 +34,7 @@ public class CAPBrowserPlugin : CAPPlugin, SFSafariViewControllerDelegate { self.vc!.preferredBarTintColor = UIColor(fromHex: toolbarColor!) } - self.bridge.viewController.present(self.vc!, animated: true, completion: { + self.bridge.presentVC(self.vc!, animated: true, completion: { call.success() }) } @@ -48,7 +48,7 @@ public class CAPBrowserPlugin : CAPPlugin, SFSafariViewControllerDelegate { call.success() } DispatchQueue.main.async { - self.bridge.viewController.dismiss(animated: true) { + self.bridge.dismissVC(animated: true) { call.success() } } diff --git a/ios/Capacitor/Capacitor/TmpViewController.swift b/ios/Capacitor/Capacitor/TmpViewController.swift new file mode 100644 index 0000000000..08917e6fd4 --- /dev/null +++ b/ios/Capacitor/Capacitor/TmpViewController.swift @@ -0,0 +1,8 @@ +import UIKit + +class TmpViewController: UIViewController { + override func viewDidAppear(_ animated: Bool) { + super.viewDidAppear(animated); + NotificationCenter.default.post(CAPBridge.tmpVCAppeared) + } +} From 03e00cc1693aea984f2a70e2ff5d01edfb4d0014 Mon Sep 17 00:00:00 2001 From: jcesarmobile Date: Thu, 2 Apr 2020 17:53:47 +0200 Subject: [PATCH 2/2] fix for ios 11 --- ios/Capacitor/Capacitor/CAPBridge.swift | 1 + ios/Capacitor/Capacitor/TmpViewController.swift | 2 +- 2 files changed, 2 insertions(+), 1 deletion(-) diff --git a/ios/Capacitor/Capacitor/CAPBridge.swift b/ios/Capacitor/Capacitor/CAPBridge.swift index 4ec3ab3383..73b93b1c91 100644 --- a/ios/Capacitor/Capacitor/CAPBridge.swift +++ b/ios/Capacitor/Capacitor/CAPBridge.swift @@ -67,6 +67,7 @@ enum BridgeError: Error { setupCordovaCompatibility() bindObservers() self.tmpWindow.rootViewController = tmpVC + self.tmpWindow.makeKeyAndVisible() NotificationCenter.default.addObserver(forName: CAPBridge.tmpVCAppeared.name, object: .none, queue: .none) { _ in self.tmpWindow.isHidden = true } diff --git a/ios/Capacitor/Capacitor/TmpViewController.swift b/ios/Capacitor/Capacitor/TmpViewController.swift index 08917e6fd4..6255511cf3 100644 --- a/ios/Capacitor/Capacitor/TmpViewController.swift +++ b/ios/Capacitor/Capacitor/TmpViewController.swift @@ -2,7 +2,7 @@ import UIKit class TmpViewController: UIViewController { override func viewDidAppear(_ animated: Bool) { - super.viewDidAppear(animated); + super.viewDidAppear(animated) NotificationCenter.default.post(CAPBridge.tmpVCAppeared) } }