Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

fix(ios): Create tmpWindow when is needed and destroy it when not needed #2995

Merged
merged 1 commit into from
May 27, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 9 additions & 9 deletions ios/Capacitor/Capacitor/CAPBridge.swift
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,7 @@ enum BridgeError: Error {

@objc public class CAPBridge : NSObject {

let tmpWindow = UIWindow.init(frame: UIScreen.main.bounds)
let tmpVC = TmpViewController.init()
var tmpWindow: UIWindow?
@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/"
Expand Down Expand Up @@ -66,10 +65,8 @@ enum BridgeError: Error {
registerPlugins()
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
self.tmpWindow = nil
}
}

Expand Down Expand Up @@ -601,16 +598,19 @@ enum BridgeError: Error {
if viewControllerToPresent.modalPresentationStyle == .popover {
self.viewController.present(viewControllerToPresent, animated: flag, completion: completion)
} else {
self.tmpWindow.makeKeyAndVisible()
self.tmpVC.present(viewControllerToPresent, animated: flag, completion: completion)
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)
}
}

@objc public func dismissVC(animated flag: Bool, completion: (() -> Void)? = nil) {
if self.tmpWindow.isHidden {
if self.tmpWindow == nil {
self.viewController.dismiss(animated: flag, completion: completion)
} else {
self.tmpVC.dismiss(animated: flag, completion: completion)
self.tmpWindow!.rootViewController!.dismiss(animated: flag, completion: completion)
self.tmpWindow = nil
}
}

Expand Down
14 changes: 13 additions & 1 deletion ios/Capacitor/Capacitor/TmpViewController.swift
Original file line number Diff line number Diff line change
@@ -1,8 +1,20 @@
import UIKit

class TmpViewController: UIViewController {
var count = 0
override func viewDidAppear(_ animated: Bool) {
super.viewDidAppear(animated)
NotificationCenter.default.post(CAPBridge.tmpVCAppeared)
// On iOS 11-12 viewDidAppear is called when TmpViewController is created and
// when the presented VC gets dismissed.
// On iOS 13 only when the presented VC gets dismissed.
// We only want to send the notification when the presented VC gets dismissed.
if #available(iOS 13, *) {
count += 2
} else {
count += 1
}
if count > 1 {
NotificationCenter.default.post(CAPBridge.tmpVCAppeared)
}
}
}