diff --git a/DuckDuckGo/MainViewController.swift b/DuckDuckGo/MainViewController.swift index 3f6e04dcb8..3544f62240 100644 --- a/DuckDuckGo/MainViewController.swift +++ b/DuckDuckGo/MainViewController.swift @@ -1355,6 +1355,18 @@ class MainViewController: UIViewController { self?.onNetworkProtectionAccountSignIn(notification) } .store(in: &vpnCancellables) + NotificationCenter.default.publisher(for: .entitlementsDidChange) + .receive(on: DispatchQueue.main) + .sink { [weak self] notification in + self?.onEntitlementsChange(notification) + } + .store(in: &vpnCancellables) + NotificationCenter.default.publisher(for: .accountDidSignOut) + .receive(on: DispatchQueue.main) + .sink { [weak self] notification in + self?.onNetworkProtectionAccountSignOut(notification) + } + .store(in: &vpnCancellables) NotificationCenter.default.publisher(for: .vpnEntitlementMessagingDidChange) .receive(on: DispatchQueue.main) @@ -1409,6 +1421,27 @@ class MainViewController: UIViewController { tunnelDefaults.resetEntitlementMessaging() os_log("[NetP Subscription] Reset expired entitlement messaging", log: .networkProtection, type: .info) } + + @objc + private func onEntitlementsChange(_ notification: Notification) { + Task { + guard case .success(false) = await AccountManager().hasEntitlement(for: .networkProtection) else { return } + + tunnelDefaults.enableEntitlementMessaging() + + let controller = NetworkProtectionTunnelController() + await controller.stop() + } + } + + @objc + private func onNetworkProtectionAccountSignOut(_ notification: Notification) { + Task { + let controller = NetworkProtectionTunnelController() + await controller.stop() + await controller.removeVPN() + } + } #endif @objc