diff --git a/App/iOS/Delegates/AppDelegate.swift b/App/iOS/Delegates/AppDelegate.swift index 876b3cd9284..488e447f17f 100644 --- a/App/iOS/Delegates/AppDelegate.swift +++ b/App/iOS/Delegates/AppDelegate.swift @@ -198,6 +198,9 @@ class AppDelegate: UIResponder, UIApplicationDelegate { // To avoid unexpected problems we clear all vpn keychain items. // New set of keychain items will be created on purchase or iap restoration. BraveVPN.clearCredentials() + + // Always load YouTube in Brave for new users + Preferences.General.keepYouTubeInBrave.value = true } if UserReferralProgram.shared != nil { diff --git a/Sources/Brave/Frontend/Browser/TabManagerNavDelegate.swift b/Sources/Brave/Frontend/Browser/TabManagerNavDelegate.swift index 357944324ae..4f86026253b 100644 --- a/Sources/Brave/Frontend/Browser/TabManagerNavDelegate.swift +++ b/Sources/Brave/Frontend/Browser/TabManagerNavDelegate.swift @@ -67,9 +67,17 @@ class TabManagerNavDelegate: NSObject, WKNavigationDelegate { } } - private func defaultAllowPolicy() -> WKNavigationActionPolicy { + private func defaultAllowPolicy(for navigationAction: WKNavigationAction) -> WKNavigationActionPolicy { let isPrivateBrowsing = tabManager?.privateBrowsingManager.isPrivateBrowsing == true - if isPrivateBrowsing || !Preferences.General.followUniversalLinks.value { + func isYouTubeLoad() -> Bool { + guard let domain = navigationAction.request.mainDocumentURL?.baseDomain else { + return false + } + let domainsWithUniversalLinks: Set = ["youtube.com", "youtu.be"] + return domainsWithUniversalLinks.contains(domain) + } + if isPrivateBrowsing || !Preferences.General.followUniversalLinks.value || + (Preferences.General.keepYouTubeInBrave.value && isYouTubeLoad()) { // Stop Brave from opening universal links by using the private enum value // `_WKNavigationActionPolicyAllowWithoutTryingAppLink` which is defined here: // https://github.com/WebKit/WebKit/blob/main/Source/WebKit/UIProcess/API/Cocoa/WKNavigationDelegatePrivate.h#L62 @@ -81,7 +89,7 @@ class TabManagerNavDelegate: NSObject, WKNavigationDelegate { @MainActor func webView(_ webView: WKWebView, decidePolicyFor navigationAction: WKNavigationAction, preferences: WKWebpagePreferences) async -> (WKNavigationActionPolicy, WKWebpagePreferences) { - var res = defaultAllowPolicy() + var res = defaultAllowPolicy(for: navigationAction) var pref = preferences for delegate in delegates { diff --git a/Sources/Brave/Frontend/ClientPreferences.swift b/Sources/Brave/Frontend/ClientPreferences.swift index 1faa2df4d42..0a2575a33b7 100644 --- a/Sources/Brave/Frontend/ClientPreferences.swift +++ b/Sources/Brave/Frontend/ClientPreferences.swift @@ -73,6 +73,9 @@ extension Preferences { /// Whether or not the app (in regular browsing mode) will follow universal links static let followUniversalLinks = Option(key: "general.follow-universal-links", default: true) + + /// Whether or not the app will always load YouTube in Brave + public static let keepYouTubeInBrave = Option(key: "general.follow-universal-links.youtube", default: false) /// Whether or not the pull-to-refresh control is added to web views static let enablePullToRefresh = Option(key: "general.enable-pull-to-refresh", default: true) diff --git a/Sources/Brave/Frontend/Settings/Display/MediaSettingsView.swift b/Sources/Brave/Frontend/Settings/Display/MediaSettingsView.swift index 10091f6e57e..bc5fe37e03b 100644 --- a/Sources/Brave/Frontend/Settings/Display/MediaSettingsView.swift +++ b/Sources/Brave/Frontend/Settings/Display/MediaSettingsView.swift @@ -10,6 +10,7 @@ import BraveUI struct MediaSettingsView: View { @ObservedObject var enableBackgroundAudio = Preferences.General.mediaAutoBackgrounding + @ObservedObject var keepYouTubeInBrave = Preferences.General.keepYouTubeInBrave var body: some View { Form { @@ -22,6 +23,10 @@ struct MediaSettingsView: View { .listRowBackground(Color(.secondaryBraveGroupedBackground)) Section(header: Text(Strings.Settings.youtube)) { + Toggle(isOn: $keepYouTubeInBrave.value) { + Text(Strings.Settings.openYouTubeInBrave) + } + .toggleStyle(SwitchToggleStyle(tint: .accentColor)) NavigationLink(destination: QualitySettingsView()) { VStack(alignment: .leading) { Text(Strings.Settings.highestQualityPlayback) diff --git a/Sources/BraveStrings/BraveStrings.swift b/Sources/BraveStrings/BraveStrings.swift index 1efb7fb70cf..3fc51d1e529 100644 --- a/Sources/BraveStrings/BraveStrings.swift +++ b/Sources/BraveStrings/BraveStrings.swift @@ -858,6 +858,15 @@ extension Strings { comment: "Header for the Youtube settings section" ) + public static let openYouTubeInBrave = + NSLocalizedString( + "settings.openYouTubeInBrave", + tableName: "BraveShared", + bundle: .module, + value: "Open YouTube links in Brave", + comment: "A toggle label which lets the user always open YouTube urls in Brave" + ) + public static let highestQualityPlayback = NSLocalizedString( "settings.highestQualityPlayback",