From b0bd27db3100850578fffdb46fdb146fa3456bc2 Mon Sep 17 00:00:00 2001 From: Shadowfacts Date: Sun, 25 Apr 2021 12:58:51 -0400 Subject: [PATCH] Fix crash when tapping non-HTTP(S) links with In-App Safari enabled --- Tusker/Info.plist | 7 ++++++- Tusker/TuskerNavigationDelegate.swift | 16 +++++++++++++--- 2 files changed, 19 insertions(+), 4 deletions(-) diff --git a/Tusker/Info.plist b/Tusker/Info.plist index f4d5d9eb..d07fd5e5 100644 --- a/Tusker/Info.plist +++ b/Tusker/Info.plist @@ -2,6 +2,11 @@ + LSApplicationQueriesSchemes + + gopher + gemini + CFBundleDevelopmentRegion $(DEVELOPMENT_LANGUAGE) CFBundleExecutable @@ -51,7 +56,7 @@ NSMicrophoneUsageDescription Post videos from the camera. NSPhotoLibraryAddUsageDescription - Save photos directly from other people's posts. + Save photos directly from other people's posts. NSPhotoLibraryUsageDescription Post photos from the photo library. NSUserActivityTypes diff --git a/Tusker/TuskerNavigationDelegate.swift b/Tusker/TuskerNavigationDelegate.swift index 39d4c353..7f2a1b8a 100644 --- a/Tusker/TuskerNavigationDelegate.swift +++ b/Tusker/TuskerNavigationDelegate.swift @@ -46,16 +46,26 @@ extension TuskerNavigationDelegate { func selected(url: URL, allowUniversalLinks: Bool = true) { func openSafari() { - if Preferences.shared.useInAppSafari { + if Preferences.shared.useInAppSafari, + url.scheme == "https" || url.scheme == "http" { let config = SFSafariViewController.Configuration() config.entersReaderIfAvailable = Preferences.shared.inAppSafariAutomaticReaderMode present(SFSafariViewController(url: url, configuration: config), animated: true) - } else { + } else if UIApplication.shared.canOpenURL(url) { UIApplication.shared.open(url, options: [:]) + } else { + var message = "The URL could not be opened." + if let scheme = url.scheme { + message += " This can happen if you do not have an app installed for '\(scheme)://' URLs." + } + let alert = UIAlertController(title: "Invalid URL", message: message, preferredStyle: .alert) + alert.addAction(UIAlertAction(title: "Ok", style: .default, handler: nil)) + present(alert, animated: true) } } - if allowUniversalLinks && Preferences.shared.openLinksInApps { + if allowUniversalLinks && Preferences.shared.openLinksInApps, + url.scheme == "https" || url.scheme == "http" { UIApplication.shared.open(url, options: [.universalLinksOnly: true]) { (success) in if (!success) { openSafari()