diff --git a/IVPNClient/AppDelegate.swift b/IVPNClient/AppDelegate.swift index e2363ecf..beb58b02 100644 --- a/IVPNClient/AppDelegate.swift +++ b/IVPNClient/AppDelegate.swift @@ -171,6 +171,99 @@ class AppDelegate: UIResponder { } } + private func userActivityConnect() { + DispatchQueue.delay(0.75) { + if UserDefaults.shared.networkProtectionEnabled { + Application.shared.connectionManager.resetRulesAndConnectShortcut(closeApp: true, actionType: .connect) + return + } + Application.shared.connectionManager.connectShortcut(closeApp: true, actionType: .connect) + } + } + + private func userActivityDisconnect() { + DispatchQueue.delay(0.75) { + if UserDefaults.shared.networkProtectionEnabled { + Application.shared.connectionManager.resetRulesAndDisconnectShortcut(closeApp: true, actionType: .disconnect) + return + } + Application.shared.connectionManager.disconnectShortcut(closeApp: true, actionType: .disconnect) + } + } + + private func userActivityAntiTrackerEnable() { + DispatchQueue.async { + if let viewController = UIApplication.topViewController() { + if Application.shared.settings.connectionProtocol.tunnelType() == .ipsec { + viewController.showAlert(title: "IKEv2 not supported", message: "AntiTracker is supported only for OpenVPN and WireGuard protocols.") { _ in + } + return + } + + UserDefaults.shared.set(true, forKey: UserDefaults.Key.isAntiTracker) + NotificationCenter.default.post(name: Notification.Name.AntiTrackerUpdated, object: nil) + if UIApplication.topViewController() as? MainViewController != nil { + NotificationCenter.default.post(name: Notification.Name.EvaluateReconnect, object: nil) + } else { + viewController.evaluateReconnect(sender: viewController.view) + } + } + } + } + + private func userActivityAntiTrackerDisable() { + DispatchQueue.async { + if let viewController = UIApplication.topViewController() { + UserDefaults.shared.set(false, forKey: UserDefaults.Key.isAntiTracker) + NotificationCenter.default.post(name: Notification.Name.AntiTrackerUpdated, object: nil) + if UIApplication.topViewController() as? MainViewController != nil { + NotificationCenter.default.post(name: Notification.Name.EvaluateReconnect, object: nil) + } else { + viewController.evaluateReconnect(sender: viewController.view) + } + } + } + } + + private func userActivityCustomDNSEnable() { + DispatchQueue.async { + if let viewController = UIApplication.topViewController() { + if Application.shared.settings.connectionProtocol.tunnelType() == .ipsec { + viewController.showAlert(title: "IKEv2 not supported", message: "Custom DNS is supported only for OpenVPN and WireGuard protocols.") { _ in + } + return + } + + guard !UserDefaults.shared.customDNS.isEmpty else { + viewController.showAlert(title: "", message: "Please enter DNS server info") + return + } + + UserDefaults.shared.set(true, forKey: UserDefaults.Key.isCustomDNS) + NotificationCenter.default.post(name: Notification.Name.CustomDNSUpdated, object: nil) + if UIApplication.topViewController() as? MainViewController != nil { + NotificationCenter.default.post(name: Notification.Name.EvaluateReconnect, object: nil) + } else { + viewController.evaluateReconnect(sender: viewController.view) + } + } + } + } + + private func userActivityCustomDNSDisable() { + DispatchQueue.async { + if let viewController = UIApplication.topViewController() { + UserDefaults.shared.set(false, forKey: UserDefaults.Key.isCustomDNS) + NotificationCenter.default.post(name: Notification.Name.CustomDNSUpdated, object: nil) + if UIApplication.topViewController() as? MainViewController != nil { + NotificationCenter.default.post(name: Notification.Name.EvaluateReconnect, object: nil) + } else { + viewController.evaluateReconnect(sender: viewController.view) + } + } + } + } + private func startPurchaseObserver() { PurchaseManager.shared.delegate = self PurchaseManager.shared.startObserver() @@ -261,6 +354,37 @@ extension AppDelegate: UIApplicationDelegate { return true } + func application(_ application: UIApplication, continue userActivity: NSUserActivity, restorationHandler: @escaping ([UIUserActivityRestoring]?) -> Void) -> Bool { + if let url = userActivity.webpageURL { + let endpoint = url.lastPathComponent + handleURLEndpoint(endpoint) + return false + } + + guard Application.shared.authentication.isLoggedIn, Application.shared.serviceStatus.isActive else { + return false + } + + switch userActivity.activityType { + case UserActivityType.Connect: + userActivityConnect() + case UserActivityType.Disconnect: + userActivityDisconnect() + case UserActivityType.AntiTrackerEnable: + userActivityAntiTrackerEnable() + case UserActivityType.AntiTrackerDisable: + userActivityAntiTrackerDisable() + case UserActivityType.CustomDNSEnable: + userActivityCustomDNSEnable() + case UserActivityType.CustomDNSDisable: + userActivityCustomDNSDisable() + default: + log(.info, message: "No such user activity") + } + + return false + } + } // MARK: - PurchaseManagerDelegate - diff --git a/IVPNClient/Scenes/MainScreen/AppIntentsHandler.swift b/IVPNClient/Scenes/MainScreen/AppIntentsHandler.swift index 5ddc8386..9e25095a 100644 --- a/IVPNClient/Scenes/MainScreen/AppIntentsHandler.swift +++ b/IVPNClient/Scenes/MainScreen/AppIntentsHandler.swift @@ -18,7 +18,7 @@ extension MainViewController { Application.shared.connectionManager.resetRulesAndConnectShortcut(closeApp: true, actionType: .connect) return } - Application.shared.connectionManager.connectShortcut(closeApp: true, actionType: .connect) + Application.shared.connectionManager.connect() } } @@ -28,80 +28,56 @@ extension MainViewController { Application.shared.connectionManager.resetRulesAndDisconnectShortcut(closeApp: true, actionType: .disconnect) return } - Application.shared.connectionManager.disconnectShortcut(closeApp: true, actionType: .disconnect) + Application.shared.connectionManager.disconnect() } } @objc func intentAntiTrackerEnable() { DispatchQueue.async { - if let viewController = UIApplication.topViewController() { - if Application.shared.settings.connectionProtocol.tunnelType() == .ipsec { - viewController.showAlert(title: "IKEv2 not supported", message: "AntiTracker is supported only for OpenVPN and WireGuard protocols.") { _ in - } - return - } - - UserDefaults.shared.set(true, forKey: UserDefaults.Key.isAntiTracker) - NotificationCenter.default.post(name: Notification.Name.AntiTrackerUpdated, object: nil) - if UIApplication.topViewController() as? MainViewController != nil { - NotificationCenter.default.post(name: Notification.Name.EvaluateReconnect, object: nil) - } else { - viewController.evaluateReconnect(sender: viewController.view) + if Application.shared.settings.connectionProtocol.tunnelType() == .ipsec { + self.showAlert(title: "IKEv2 not supported", message: "AntiTracker is supported only for OpenVPN and WireGuard protocols.") { _ in } + return } + + UserDefaults.shared.set(true, forKey: UserDefaults.Key.isAntiTracker) + NotificationCenter.default.post(name: Notification.Name.AntiTrackerUpdated, object: nil) + self.evaluateReconnect(sender: self.view) } } @objc func intentAntiTrackerDisable() { DispatchQueue.async { - if let viewController = UIApplication.topViewController() { - UserDefaults.shared.set(false, forKey: UserDefaults.Key.isAntiTracker) - NotificationCenter.default.post(name: Notification.Name.AntiTrackerUpdated, object: nil) - if UIApplication.topViewController() as? MainViewController != nil { - NotificationCenter.default.post(name: Notification.Name.EvaluateReconnect, object: nil) - } else { - viewController.evaluateReconnect(sender: viewController.view) - } - } + UserDefaults.shared.set(false, forKey: UserDefaults.Key.isAntiTracker) + NotificationCenter.default.post(name: Notification.Name.AntiTrackerUpdated, object: nil) + self.evaluateReconnect(sender: self.view) } } @objc func intentCustomDNSEnable() { DispatchQueue.async { - if let viewController = UIApplication.topViewController() { - if Application.shared.settings.connectionProtocol.tunnelType() == .ipsec { - viewController.showAlert(title: "IKEv2 not supported", message: "Custom DNS is supported only for OpenVPN and WireGuard protocols.") { _ in - } - return - } - - guard !UserDefaults.shared.customDNS.isEmpty else { - viewController.showAlert(title: "", message: "Please enter DNS server info") - return - } - - UserDefaults.shared.set(true, forKey: UserDefaults.Key.isCustomDNS) - NotificationCenter.default.post(name: Notification.Name.CustomDNSUpdated, object: nil) - if UIApplication.topViewController() as? MainViewController != nil { - NotificationCenter.default.post(name: Notification.Name.EvaluateReconnect, object: nil) - } else { - viewController.evaluateReconnect(sender: viewController.view) + if Application.shared.settings.connectionProtocol.tunnelType() == .ipsec { + self.showAlert(title: "IKEv2 not supported", message: "Custom DNS is supported only for OpenVPN and WireGuard protocols.") { _ in } + return + } + + guard !UserDefaults.shared.customDNS.isEmpty else { + self.showAlert(title: "", message: "Please enter DNS server info") + return } + + UserDefaults.shared.set(true, forKey: UserDefaults.Key.isCustomDNS) + NotificationCenter.default.post(name: Notification.Name.CustomDNSUpdated, object: nil) + self.evaluateReconnect(sender: self.view) } } @objc func intentCustomDNSDisable() { DispatchQueue.async { - if let viewController = UIApplication.topViewController() { - UserDefaults.shared.set(false, forKey: UserDefaults.Key.isCustomDNS) - NotificationCenter.default.post(name: Notification.Name.CustomDNSUpdated, object: nil) - if UIApplication.topViewController() as? MainViewController != nil { - NotificationCenter.default.post(name: Notification.Name.EvaluateReconnect, object: nil) - } else { - viewController.evaluateReconnect(sender: viewController.view) - } - } + UserDefaults.shared.set(false, forKey: UserDefaults.Key.isCustomDNS) + NotificationCenter.default.post(name: Notification.Name.CustomDNSUpdated, object: nil) + self.evaluateReconnect(sender: self.view) } }