diff --git a/BalanceOpen.xcodeproj/project.pbxproj b/BalanceOpen.xcodeproj/project.pbxproj index 0871cad5..10c4781c 100644 --- a/BalanceOpen.xcodeproj/project.pbxproj +++ b/BalanceOpen.xcodeproj/project.pbxproj @@ -995,7 +995,7 @@ DEVELOPMENT_TEAM = Q4G66XSS36; INFOPLIST_FILE = AutoLaunchBalanceHelper/Info.plist; LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/../Frameworks"; - PRODUCT_BUNDLE_IDENTIFIER = balance.money.AutoLaunchBalanceHelper; + PRODUCT_BUNDLE_IDENTIFIER = "software.balanced.balance-open-autolaunch"; PRODUCT_NAME = "$(TARGET_NAME)"; PROVISIONING_PROFILE_SPECIFIER = ""; SKIP_INSTALL = YES; @@ -1011,7 +1011,7 @@ DEVELOPMENT_TEAM = Q4G66XSS36; INFOPLIST_FILE = AutoLaunchBalanceHelper/Info.plist; LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/../Frameworks"; - PRODUCT_BUNDLE_IDENTIFIER = balance.money.AutoLaunchBalanceHelper; + PRODUCT_BUNDLE_IDENTIFIER = "software.balanced.balance-open-autolaunch"; PRODUCT_NAME = "$(TARGET_NAME)"; PROVISIONING_PROFILE_SPECIFIER = ""; SKIP_INSTALL = YES; diff --git a/BalanceOpen/AppDelegate.swift b/BalanceOpen/AppDelegate.swift index a8dee887..bd3c5f9a 100644 --- a/BalanceOpen/AppDelegate.swift +++ b/BalanceOpen/AppDelegate.swift @@ -9,6 +9,8 @@ import Cocoa import ServiceManagement +let autolaunchBundleId = "software.balanced.balance-open-autolaunch" + class AppDelegate: NSObject, NSApplicationDelegate { // @@ -21,6 +23,12 @@ class AppDelegate: NSObject, NSApplicationDelegate { var contentViewController: PopoverViewController! var preferencesWindowController: NSWindowController! + var launchedAtLogin = false + var isAutolaunchHelperRunning: Bool { + let appsRunning = NSRunningApplication.runningApplications(withBundleIdentifier: autolaunchBundleId) + return appsRunning.count > 0 + } + var pinned: Bool { get { return statusItem.windowConfiguration.isPinned @@ -84,8 +92,8 @@ class AppDelegate: NSObject, NSApplicationDelegate { NSAppleEventManager.shared().setEventHandler(self, andSelector: #selector(AppDelegate.handleURLEvent(event:withReply:)), forEventClass: AEEventClass(kInternetEventClass), andEventID: AEEventID(kAEGetURL) ) - // Query the helper app to see if we were auto launched (dirty hack because Apple makes simple shit difficult) - forceAutoLaunch() + // Check for the helper app to see if we were auto launched + launchedAtLogin = isAutolaunchHelperRunning // Initialize singletons initializeSingletons() @@ -221,10 +229,10 @@ class AppDelegate: NSObject, NSApplicationDelegate { } } - //DO WE NEED THIS? -// if !autoLaunch.wasLaunchedAtLogin && !showedAlert { -// self.showPopover() -// } + // Show Popover if we are manually lauching the app + if !self.launchedAtLogin && !showedAlert { + self.showPopover() + } } } @@ -333,12 +341,6 @@ class AppDelegate: NSObject, NSApplicationDelegate { NSApp.terminate(nil) } - func forceAutoLaunch() { - if (!SMLoginItemSetEnabled("balance.money.AutoLaunchBalanceHelper" as CFString, defaults.launchAtLogin)) { - print("Auto login was not successful"); - } - } - func sendFeedback() { let urlString = "https://github.com/balancemymoney/BalanceForBlockchain/issues" _ = try? NSWorkspace.shared.open(URL(string: urlString)!, options: [], configuration: [:]) diff --git a/BalanceOpen/Data Model/Static & Singletons/Defaults.swift b/BalanceOpen/Data Model/Static & Singletons/Defaults.swift index a28e0ea2..8708ffb0 100644 --- a/BalanceOpen/Data Model/Static & Singletons/Defaults.swift +++ b/BalanceOpen/Data Model/Static & Singletons/Defaults.swift @@ -7,6 +7,7 @@ // import Foundation +import ServiceManagement class Defaults { internal struct Keys { @@ -46,7 +47,17 @@ class Defaults { return defaults.bool(forKey: Keys.launchAtLogin) } set { - defaults.set(newValue, forKey: Keys.launchAtLogin) + if SMLoginItemSetEnabled(autolaunchBundleId as CFString, newValue) { + defaults.set(newValue, forKey: Keys.launchAtLogin) + } else { + log.severe("Failed to set login at launch preference") + + let alert = NSAlert() + alert.alertStyle = .warning + alert.messageText = newValue ? "Unable to create the login item" : "Unable to remove the login item" + alert.addButton(withTitle: "OK") + alert.runModal() + } } }