From 7691228ce8e11ea67e8642a33cf685c59a1c4c44 Mon Sep 17 00:00:00 2001 From: Fernando Bunn Date: Tue, 26 Sep 2023 16:41:31 +0100 Subject: [PATCH] Fix URL deeplink (#2035) --- Core/AppDeepLinkSchemes.swift | 13 ++++++++++++- DuckDuckGo/AppDelegate+AppDeepLinks.swift | 13 ++++++++++--- 2 files changed, 22 insertions(+), 4 deletions(-) diff --git a/Core/AppDeepLinkSchemes.swift b/Core/AppDeepLinkSchemes.swift index 55e6868e46..c88f697cbc 100644 --- a/Core/AppDeepLinkSchemes.swift +++ b/Core/AppDeepLinkSchemes.swift @@ -45,10 +45,21 @@ public enum AppDeepLinkSchemes: String, CaseIterable { } public static func query(fromQuickLink url: URL) -> String { - return url.absoluteString + let query = url.absoluteString .replacingOccurrences(of: AppDeepLinkSchemes.quickLink.url.absoluteString, with: "", options: .caseInsensitive) + + return AppDeepLinkSchemes.fixURLScheme(query) } + private static func fixURLScheme(_ urlString: String) -> String { + let pattern = "^https?//" + + if urlString.range(of: pattern, options: .regularExpression) != nil { + return urlString.replacingOccurrences(of: "//", with: "://") + } else { + return urlString + } + } } diff --git a/DuckDuckGo/AppDelegate+AppDeepLinks.swift b/DuckDuckGo/AppDelegate+AppDeepLinks.swift index 56a36880a1..1c90feac0e 100644 --- a/DuckDuckGo/AppDelegate+AppDeepLinks.swift +++ b/DuckDuckGo/AppDelegate+AppDeepLinks.swift @@ -31,13 +31,11 @@ extension AppDelegate { mainViewController.newTab(reuseExisting: true) mainViewController.enterSearch() - case .favorites: mainViewController.newTab(reuseExisting: true, allowingKeyboard: false) case .quickLink: - let query = AppDeepLinkSchemes.query(fromQuickLink: url) - mainViewController.loadQueryInNewTab(query, reuseExisting: true) + handleQuickLink(url, mainViewController) case .addFavorite: mainViewController.startAddFavoriteFlow() @@ -67,4 +65,13 @@ extension AppDelegate { return true } + private func handleQuickLink(_ url: URL, _ mainViewController: MainViewController) { + let query = AppDeepLinkSchemes.query(fromQuickLink: url) + if let url = URL(string: query) { + mainViewController.loadUrlInNewTab(url, reuseExisting: true, inheritedAttribution: nil) + } else { + mainViewController.loadQueryInNewTab(query, reuseExisting: true) + } + } + }