OS X app doesn't launch new window on dock icon press in Swift
Add this to your App Delegate:
func applicationShouldHandleReopen(theApplication: NSApplication, hasVisibleWindows flag: Bool) -> Bool {
if !flag {
for window: AnyObject in theApplication.windows {
window.makeKeyAndOrderFront(self)
}
}
return true
}
How do you make the Application window open when the dock icon is clicked?
Implement - (BOOL)applicationShouldHandleReopen:(NSApplication *)theApplication hasVisibleWindows:(BOOL)flag
in your app delegate. Check the documentation for the details of the return value.
Document based apps and non-document based apps behave slightly differently. If there are no open windows when the dock icon of a document based app is clicked then it will create a new document. If there are no open windows when the dock icon of a non-document based app is clicked then it will do nothing.
How to Make macOS App Window Hidden When Closed and Reopened With Menu Bar Item?
Don't use the Application is agent
approach, but change the activationPolicy
of the NSApp
.
To dynamically hide the icon after closing the (last) window use this in your AppDelegate:
func applicationShouldTerminateAfterLastWindowClosed(_ sender: NSApplication) -> Bool {
NSApp.setActivationPolicy(.accessory)
return false
}
And use something simular to this to initialise your menubar icon and activate the window including a dock icon:
class ViewController: NSViewController {
var status: NSStatusItem?
override func viewDidLoad() {
super.viewDidLoad()
status = NSStatusBar.system.statusItem(withLength: NSStatusItem.variableLength)
status?.button?.title = "Test"
status?.button?.action = #selector(activateWindow(_:))
status?.button?.target = self
}
@IBAction func activateWindow(_ sender: AnyObject) {
NSApp.setActivationPolicy(.regular)
DispatchQueue.main.async {
NSApp.windows.first?.orderFrontRegardless()
}
}
}
Window won't restore after it is closed
There isn't much information in the question, but let me presume that the app is a single-window app (i.e., not NSDocument
-based).
A typical situation in this scenario is that the user closes the window, and the appliction keeps running with the icon in the Dock as it is supposed to.
In that situation, the user would normally like the window to re-appear when the app is activated by clicking the icon in the Dock.
To obtain that, you can implement applicationShouldHandleReopen:hasVisibleWindows:
as follows:
- (BOOL)applicationShouldHandleReopen:(NSApplication *)theApplication hasVisibleWindows:(BOOL)visibleWindows
{
if ( visibleWindows ) {
[self.window orderFront:self];
}
else {
[self.window makeKeyAndOrderFront:self];
}
return YES;
}
Macos app is getting closed when there're no windows open
Set NSSupportsAutomaticTermination to NO in your Info.plist.
macOS menubar application: main menu not being displayed
This is my workaround solution for now:
As I wrote in the question, if I click on another app and come back to my app, the menubar is being displayed. I am simulating this when I try to show the preference window:
NSApp.setActivationPolicy(.regular)
NSApp.activate(ignoringOtherApps: true)
window.makeKeyAndOrderFront(nil)
if (NSRunningApplication.runningApplications(withBundleIdentifier: "com.apple.dock").first?.activate(options: []))! {
let deadlineTime = DispatchTime.now() + .milliseconds(200)
DispatchQueue.main.asyncAfter(deadline: deadlineTime) {
NSApp.setActivationPolicy(.regular)
NSApp.activate(ignoringOtherApps: true)
}
}
This is not a perfect solution. If I don't find a better solution, I will file a bug.
Related Topics
(Key: Anyobject, Value: Anyobject)' Does Not Have a Member Named 'subscript'
Access Each Header and Controls in The Tableview in Swift
How to Get Unsaferawpointer on The Swift Object
Swift Loaditem Closure Not Running
Underlying Type for Tuple in Swift
Understanding Optional Global Variables in Swift
Why Is Swift Giving Me Inaccurate Floating Point Arithmetic Results
Swift Casting Generic to Optional with a Nil Value Causes Fatalerror
Comma Automatically Being Added to Textfield in Swift
Sharing Button Works Perfectly on iPhone But Crash on Ipad
Cast While Looping Over Dictionary in Swift
Dealing with Octal Numbers in Swift
How to Create Rounded Image with Border and Shadow as Mkannotationview in Swift
How to Stretch a View to Its Parent Frame with Swiftui