Skip to content
This repository has been archived by the owner on Oct 27, 2022. It is now read-only.

Commit

Permalink
Fix detection of if another instance of the app is running
Browse files Browse the repository at this point in the history
Show alert if another instance of the app is running and terminate
  • Loading branch information
anjannath authored and guillaumerose committed Mar 16, 2021
1 parent f248caf commit 27b189b
Showing 1 changed file with 12 additions and 6 deletions.
18 changes: 12 additions & 6 deletions CodeReady Containers/AppDelegate.swift
Original file line number Diff line number Diff line change
Expand Up @@ -55,13 +55,19 @@ class AppDelegate: NSObject, NSApplicationDelegate, NSMenuDelegate {
}
menu.delegate = self
statusItem.menu = self.menu

let applications = NSWorkspace.shared.runningApplications

for app in applications where app == NSWorkspace.shared.self {
NSApplication.shared.terminate(self)

if NSRunningApplication.runningApplications(withBundleIdentifier: Bundle.main.bundleIdentifier!).count > 1 {
print("more than one running")
let alert = NSAlert()
alert.addButton(withTitle: "OK")
alert.messageText = "Another instance of \(Bundle.main.bundleIdentifier!) is running."
alert.informativeText = "This instance will now terminate."
alert.alertStyle = .critical
alert.runModal()

NSApp.terminate(self)
}

let center = UNUserNotificationCenter.current()
center.requestAuthorization(options: [.alert, .sound], completionHandler: { (granted, error) in
notificationAllowed = granted
Expand Down

0 comments on commit 27b189b

Please sign in to comment.