From 8191f2476aee10d3b97210fd2eccec13a2028433 Mon Sep 17 00:00:00 2001 From: Daniel Jalkut Date: Tue, 22 Jun 2021 12:35:09 -0400 Subject: [PATCH] Add x-netnewswire-feed to the list of scheme prefixes for adding a feed to NetNewsWire, and modify the behavior of the Safari App Extension plugin so that it consults the host app preference for opening in NetNewsWire or with the default feed handler, before choosing which scheme to prepend to the feed URL. --- Mac/Resources/Info.plist | 1 + .../SafariExtensionHandler.swift | 21 ++++++++++++++----- .../Subscribe_to_Feed.entitlements | 4 ++++ 3 files changed, 21 insertions(+), 5 deletions(-) diff --git a/Mac/Resources/Info.plist b/Mac/Resources/Info.plist index af856875b..e6a304af3 100644 --- a/Mac/Resources/Info.plist +++ b/Mac/Resources/Info.plist @@ -33,6 +33,7 @@ feed feeds + x-netnewswire-feed diff --git a/Mac/SafariExtension/SafariExtensionHandler.swift b/Mac/SafariExtension/SafariExtensionHandler.swift index 44c560bf7..b3c8cf828 100644 --- a/Mac/SafariExtension/SafariExtensionHandler.swift +++ b/Mac/SafariExtension/SafariExtensionHandler.swift @@ -40,13 +40,24 @@ class SafariExtensionHandler: SFSafariExtensionHandler { } } - override func messageReceived(withName messageName: String, from page: SFSafariPage, userInfo: [String : Any]?) { + override func messageReceived(withName messageName: String, from page: SFSafariPage, userInfo: [String : Any]?) { if (messageName == "subscribeToFeed") { - if let feedURLString = userInfo?["url"] as? String { + if var feedURLString = userInfo?["url"] as? String { + // Ask for the user default from NetNewsWire's defaults to determine whether to open the feed URL + // using whatever the system configured default is, or to always hard-code it to NetNewsWire itself. + if let pluginBundleID = Bundle.main.bundleIdentifier { + // By convention we assume that our bundle ID will always be the same as the host app's, with + // the addition of ".Subscribe-to-Feed". + let hostAppBundleID = pluginBundleID.replacingOccurrences(of: ".Subscribe-to-Feed", with: "") + + if let sharedDefaults = UserDefaults(suiteName: hostAppBundleID) { + let openInNNW = sharedDefaults.bool(forKey: "subscribeToFeedsInNetNewsWire") + if openInNNW { + feedURLString = feedURLString.replacingOccurrences(of: "feed:", with: "x-netnewswire-feed") + } + } + } if let feedURL = URL(string: feedURLString) { - // We could do something more NetNewsWire-specific like invoke an app-specific scheme - // to subscribe in the app. For starters we just let NSWorkspace open the URL in the - // default "feed:" URL scheme handler. NSWorkspace.shared.open(feedURL) } } diff --git a/Mac/SafariExtension/Subscribe_to_Feed.entitlements b/Mac/SafariExtension/Subscribe_to_Feed.entitlements index 852fa1a47..08aae1850 100644 --- a/Mac/SafariExtension/Subscribe_to_Feed.entitlements +++ b/Mac/SafariExtension/Subscribe_to_Feed.entitlements @@ -4,5 +4,9 @@ com.apple.security.app-sandbox + com.apple.security.application-groups + + group.$(ORGANIZATION_IDENTIFIER).NetNewsWire-Evergreen +