From 04faf57d4c7586066166a1570b2bb3fdb17becda Mon Sep 17 00:00:00 2001 From: Davide De Rosa Date: Mon, 18 Jul 2022 02:27:56 +0200 Subject: [PATCH] Send app to background if started hidden Sandbox had to be enabled in order to submit binary to App Store Connect, therefore command line arguments cannot be used to tell if the app was started by the launcher. However, given that launcher starts app in hidden state, we can safely assert that if the app is hidden on start, it was started by the launcher. See f33380b4e25e54f22365dc99ad30b9e2359dbec3 Also drop automatic signing on Mac bundle and unused utils. --- Passepartout.xcodeproj/project.pbxproj | 6 ++++-- Passepartout/App/AppDelegate.swift | 2 +- Passepartout/AppShared/Mac/MacUtils.swift | 6 +----- Passepartout/Constants.swift | 2 -- Passepartout/Launcher/AppDelegate.swift | 4 +++- Passepartout/Launcher/Launcher.entitlements | 2 +- Passepartout/Mac/Mac/DefaultMacUtils.swift | 14 ++------------ 7 files changed, 12 insertions(+), 24 deletions(-) diff --git a/Passepartout.xcodeproj/project.pbxproj b/Passepartout.xcodeproj/project.pbxproj index d31bb0dd..354985a3 100644 --- a/Passepartout.xcodeproj/project.pbxproj +++ b/Passepartout.xcodeproj/project.pbxproj @@ -1800,7 +1800,7 @@ CLANG_CXX_LANGUAGE_STANDARD = "gnu++17"; CLANG_ENABLE_MODULES = YES; CODE_SIGN_IDENTITY = "-"; - CODE_SIGN_STYLE = Automatic; + CODE_SIGN_STYLE = Manual; COMBINE_HIDPI_IMAGES = YES; COPY_PHASE_STRIP = NO; CURRENT_PROJECT_VERSION = 3284; @@ -1816,6 +1816,7 @@ MTL_FAST_MATH = YES; PRODUCT_BUNDLE_IDENTIFIER = "$(CFG_MAC_ID)"; PRODUCT_NAME = "$(TARGET_NAME)"; + PROVISIONING_PROFILE_SPECIFIER = ""; SDKROOT = macosx; SKIP_INSTALL = YES; SWIFT_EMIT_LOC_STRINGS = YES; @@ -1832,7 +1833,7 @@ CLANG_CXX_LANGUAGE_STANDARD = "gnu++17"; CLANG_ENABLE_MODULES = YES; CODE_SIGN_IDENTITY = "-"; - CODE_SIGN_STYLE = Automatic; + CODE_SIGN_STYLE = Manual; COMBINE_HIDPI_IMAGES = YES; COPY_PHASE_STRIP = NO; CURRENT_PROJECT_VERSION = 3284; @@ -1848,6 +1849,7 @@ MTL_FAST_MATH = YES; PRODUCT_BUNDLE_IDENTIFIER = "$(CFG_MAC_ID)"; PRODUCT_NAME = "$(TARGET_NAME)"; + PROVISIONING_PROFILE_SPECIFIER = ""; SDKROOT = macosx; SKIP_INSTALL = YES; SWIFT_EMIT_LOC_STRINGS = YES; diff --git a/Passepartout/App/AppDelegate.swift b/Passepartout/App/AppDelegate.swift index 875b1aec..c0e75792 100644 --- a/Passepartout/App/AppDelegate.swift +++ b/Passepartout/App/AppDelegate.swift @@ -33,7 +33,7 @@ class AppDelegate: NSObject, UIApplicationDelegate, ObservableObject { #if targetEnvironment(macCatalyst) mac.configure() mac.menu.install() - if ProcessInfo.processInfo.arguments.contains(Constants.Global.appArgumentBackground) { + if mac.utils.isStartedByLauncher { mac.utils.sendAppToBackground() } #endif diff --git a/Passepartout/AppShared/Mac/MacUtils.swift b/Passepartout/AppShared/Mac/MacUtils.swift index 3858f5e7..712a5513 100644 --- a/Passepartout/AppShared/Mac/MacUtils.swift +++ b/Passepartout/AppShared/Mac/MacUtils.swift @@ -27,12 +27,8 @@ import Foundation @objc public protocol MacUtils { - var isForeground: Bool { get } + var isStartedByLauncher: Bool { get } - func toggleForeground() - - func bringAppToForeground() - func sendAppToBackground() } diff --git a/Passepartout/Constants.swift b/Passepartout/Constants.swift index 7c57c243..bef96aa8 100644 --- a/Passepartout/Constants.swift +++ b/Passepartout/Constants.swift @@ -44,7 +44,5 @@ enum Constants { static let appBuildNumber = Int(Bundle.main.infoDictionary![kCFBundleVersionKey as String] as! String)! static let appVersionString = "\(appVersionNumber) (\(appBuildNumber))" - - static let appArgumentBackground = "-background" } } diff --git a/Passepartout/Launcher/AppDelegate.swift b/Passepartout/Launcher/AppDelegate.swift index 49d60f84..deb18897 100644 --- a/Passepartout/Launcher/AppDelegate.swift +++ b/Passepartout/Launcher/AppDelegate.swift @@ -40,9 +40,11 @@ class AppDelegate: NSObject, NSApplicationDelegate, ObservableObject { NSApp.terminate(self) return } + let cfg = NSWorkspace.OpenConfiguration() cfg.hides = true - cfg.arguments = [Constants.Global.appArgumentBackground] + cfg.activates = false + cfg.addsToRecentItems = false NSWorkspace.shared.openApplication(at: appURL, configuration: cfg) { app, error in if let error = error { NSLog("Unable to launch main app: \(error)") diff --git a/Passepartout/Launcher/Launcher.entitlements b/Passepartout/Launcher/Launcher.entitlements index e89b7f32..852fa1a4 100644 --- a/Passepartout/Launcher/Launcher.entitlements +++ b/Passepartout/Launcher/Launcher.entitlements @@ -3,6 +3,6 @@ com.apple.security.app-sandbox - + diff --git a/Passepartout/Mac/Mac/DefaultMacUtils.swift b/Passepartout/Mac/Mac/DefaultMacUtils.swift index 59789792..1a1bd896 100644 --- a/Passepartout/Mac/Mac/DefaultMacUtils.swift +++ b/Passepartout/Mac/Mac/DefaultMacUtils.swift @@ -27,20 +27,10 @@ import Foundation import AppKit class DefaultMacUtils: MacUtils { + lazy private(set) var isStartedByLauncher = NSApp.isHidden + private let transformer = ObservableProcessTransformer.shared - var isForeground: Bool { - transformer.isForeground - } - - func toggleForeground() { - transformer.toggleForeground() - } - - func bringAppToForeground() { - transformer.bringToForeground() - } - func sendAppToBackground() { transformer.sendToBackground() }