diff options
| -rw-r--r-- | ios/MullvadVPN/AppDelegate.swift | 45 | ||||
| -rw-r--r-- | ios/MullvadVPN/SceneDelegate.swift | 19 |
2 files changed, 46 insertions, 18 deletions
diff --git a/ios/MullvadVPN/AppDelegate.swift b/ios/MullvadVPN/AppDelegate.swift index f1958e9955..8776da06e9 100644 --- a/ios/MullvadVPN/AppDelegate.swift +++ b/ios/MullvadVPN/AppDelegate.swift @@ -53,6 +53,7 @@ class AppDelegate: UIResponder, UIApplicationDelegate { registerBackgroundTasks() setupPaymentHandler() setupNotificationHandler() + addApplicationNotifications(application: application) let setupTunnelManagerOperation = AsyncBlockOperation(dispatchQueue: .main) { operation in TunnelManager.shared.loadConfiguration { error in @@ -117,6 +118,25 @@ class AppDelegate: UIResponder, UIApplicationDelegate { // the discarded scenes, as they will not return. } + // MARK: - Notifications + + @objc private func didBecomeActive(_ notification: Notification) { + TunnelManager.shared.refreshTunnelStatus() + TunnelManager.shared.startPeriodicPrivateKeyRotation() + RelayCacheTracker.shared.startPeriodicUpdates() + AddressCacheTracker.shared.startPeriodicUpdates() + } + + @objc private func willResignActive(_ notification: Notification) { + TunnelManager.shared.stopPeriodicPrivateKeyRotation() + RelayCacheTracker.shared.stopPeriodicUpdates() + AddressCacheTracker.shared.stopPeriodicUpdates() + } + + @objc private func didEnterBackground(_ notification: Notification) { + scheduleBackgroundTasks() + } + // MARK: - Background tasks private func registerBackgroundTasks() { @@ -194,7 +214,7 @@ class AppDelegate: UIResponder, UIApplicationDelegate { } } - func scheduleBackgroundTasks() { + private func scheduleBackgroundTasks() { scheduleAppRefreshTask() scheduleKeyRotationTask() scheduleAddressCacheUpdateTask() @@ -266,6 +286,29 @@ class AppDelegate: UIResponder, UIApplicationDelegate { // MARK: - Private + private func addApplicationNotifications(application: UIApplication) { + let notificationCenter = NotificationCenter.default + + notificationCenter.addObserver( + self, + selector: #selector(didBecomeActive(_:)), + name: UIApplication.didBecomeActiveNotification, + object: application + ) + notificationCenter.addObserver( + self, + selector: #selector(willResignActive(_:)), + name: UIApplication.willResignActiveNotification, + object: application + ) + notificationCenter.addObserver( + self, + selector: #selector(didEnterBackground(_:)), + name: UIApplication.didEnterBackgroundNotification, + object: application + ) + } + private func setupPaymentHandler() { StorePaymentManager.shared.delegate = self StorePaymentManager.shared.addPaymentObserver(TunnelManager.shared) diff --git a/ios/MullvadVPN/SceneDelegate.swift b/ios/MullvadVPN/SceneDelegate.swift index 3cdb5d6b5c..1113eefb18 100644 --- a/ios/MullvadVPN/SceneDelegate.swift +++ b/ios/MullvadVPN/SceneDelegate.swift @@ -119,8 +119,6 @@ extension SceneDelegate: UIWindowSceneDelegate { } func sceneDidBecomeActive(_ scene: UIScene) { - TunnelManager.shared.refreshTunnelStatus() - if isSceneConfigured { accountDataThrottling.requestUpdate( condition: settingsNavController == nil @@ -129,31 +127,18 @@ extension SceneDelegate: UIWindowSceneDelegate { ) } - RelayCacheTracker.shared.startPeriodicUpdates() - TunnelManager.shared.startPeriodicPrivateKeyRotation() - AddressCacheTracker.shared.startPeriodicUpdates() ShortcutsManager.shared.updateVoiceShortcuts() setShowsPrivacyOverlay(false) } func sceneWillResignActive(_ scene: UIScene) { - RelayCacheTracker.shared.stopPeriodicUpdates() - TunnelManager.shared.stopPeriodicPrivateKeyRotation() - AddressCacheTracker.shared.stopPeriodicUpdates() - setShowsPrivacyOverlay(true) } - func sceneWillEnterForeground(_ scene: UIScene) { - // no-op - } - - func sceneDidEnterBackground(_ scene: UIScene) { - let appDelegate = UIApplication.shared.delegate as? AppDelegate + func sceneWillEnterForeground(_ scene: UIScene) {} - appDelegate?.scheduleBackgroundTasks() - } + func sceneDidEnterBackground(_ scene: UIScene) {} } // MARK: - SettingsButtonInteractionDelegate |
