diff --git a/ios/Capacitor/Capacitor/CapacitorBridge.swift b/ios/Capacitor/Capacitor/CapacitorBridge.swift index 015d141e64..fe34de36ea 100644 --- a/ios/Capacitor/Capacitor/CapacitorBridge.swift +++ b/ios/Capacitor/Capacitor/CapacitorBridge.swift @@ -106,6 +106,8 @@ internal class CapacitorBridge: NSObject, CAPBridgeProtocol { // Background dispatch queue for plugin calls var dispatchQueue = DispatchQueue(label: "bridge") + // Array of block based observers + var observers: [NSObjectProtocol] = [] // MARK: - CAPBridgeProtocol: Deprecated @@ -189,14 +191,17 @@ internal class CapacitorBridge: NSObject, CAPBridgeProtocol { exportCoreJS(localUrl: configuration.localURL.absoluteString) registerPlugins() setupCordovaCompatibility() - NotificationCenter.default.addObserver(forName: type(of: self).tmpVCAppeared.name, object: .none, queue: .none) { [weak self] _ in + observers.append(NotificationCenter.default.addObserver(forName: type(of: self).tmpVCAppeared.name, object: .none, queue: .none) { [weak self] _ in self?.tmpWindow = nil - } + }) } deinit { // the message handler needs to removed to avoid any retain cycles webViewDelegationHandler.cleanUp() + for observer in observers { + NotificationCenter.default.removeObserver(observer) + } } // MARK: - Plugins @@ -220,6 +225,13 @@ internal class CapacitorBridge: NSObject, CAPBridgeProtocol { if injectCordovaFiles { exportCordovaJS() registerCordovaPlugins() + } else { + observers.append(NotificationCenter.default.addObserver(forName: UIApplication.didBecomeActiveNotification, object: nil, queue: OperationQueue.main) { [weak self] (_) in + self?.triggerDocumentJSEvent(eventName: "resume") + }) + observers.append(NotificationCenter.default.addObserver(forName: UIApplication.willResignActiveNotification, object: nil, queue: OperationQueue.main) { [weak self] (_) in + self?.triggerDocumentJSEvent(eventName: "pause") + }) } }