Swift MacOS - Is there a way to block access(stop focus) to background apps when my app is opened? - swift

I'm fairly new to Mac Development and now I'm developing a Swift application for desktop. The application consist of a window, which has a webview in it. Is there any possiblity that when my window opens it always stays at top, and you cannot click on any other windows bellow it.
For now I have made it this far with my code:
func applicationWillHide(_ notification: Notification) {
NSApplication.shared().activate(ignoringOtherApps: true)
}
func applicationDidResignActive(_ notification: Notification) {
NSApplication.shared().activate(ignoringOtherApps: true)
}
I'm using applicationWillHide and applicationDidResignActive to bring the window back to the top when a user clicks outside of it, but its still possible to focus on background apps, although they are not at the top. Can this "stealing" of focus somehow be disabled?

Related

macOS native coding with browsers, how to open a url in existing tab from obj-c/swift?

I've developed a command palette for macOS. Over the last few days I've figured out a way to show the Notification Center inside my app.
I would like to mimic the behaviour of the real Notification Center when clicking a notification: open the corresponding tab when clicking in a browser notification.
However whenever I cannot figure out a way to do it programmatically, I can only tell macOS to open the link and it automatically opens the default browser and creates a new tab.
let url = URL(string: "https://www.google.com")! // this opens a new tab everytime
if NSWorkspace.shared.open(url) {
print("default browser was successfully opened")
}
As for the notification itself I do get all the info it contains: the app bundle id, the notification payload, and even a link (e.g. n#https://web.whatsapp.com#465718293123#c.us). I've seen some answers that rely on AppleScript but I would rather avoid it if possible. But if not possible... then happy to fallback to it.
Any ideas how to achieve this? Many thanks!
P.D. I've also tried some variations of the link such as: arc://web.whatsapp.com#1234567 or arc://n#web.whatsapp.com#1234567. At most this focuses on the browser but not on the tab.

MacOS Cocoa - keep dock icon open when all windows are closed

my application is a listener service that is normally idle, waiting for incoming messages over the network, or for instructions from the user via the Dock icon menus.
however something keeps closing or hiding the Dock icon, unless i keep a window open.
i am working in XCode Swift, with Cocoa with Storyboards, and am developing for MacOS 10.13
i started a new project as above, and edited AppDelegate.swift as follows:
#NSApplicationMain
class AppDelegate: NSObject, NSApplicationDelegate {
func applicationDidFinishLaunching(_ aNotification: Notification) {
print( "app did finish launching" )
// Insert code here to initialize your application
for window in NSApp.windows {
print( window.debugDescription )
window.close()
}
DispatchQueue.global(qos: .userInitiated).async {
var seconds = 0
while true {
sleep( 1 )
print( ( seconds % 2 ) == 0 ? "tick" : "tock", seconds )
seconds += 1
}
}
}
func applicationShouldTerminateAfterLastWindowClosed(_ sender: NSApplication) -> Bool {
print( "app should terminate after last window - set regular policy" )
sender.setActivationPolicy( .regular )
return false
}
}
with no other changes, the application builds and runs, closing one window, however;
1 - the dock icon vanishes as soon as the application loses focus.
2 - after about sixty seconds the application slows down, and the sleep call is taking much longer than 1 second to return.
i need to prevent both of these behaviours, from which two questions follow:
a - where does Apple document these behaviours?
b - what is the correct way to control these behaviours?
in my experience it seems that most MacOS apps stay open in the dock, even though all of their windows are closed. for example Finder, Terminal, Safari, Notes, etc, etc.
what am i not understanding?
edited - i found this answer a week ago
Mac app disappears when window is closed then another app is selected
and i added both of parameters to my application Info, however it would seem i made an error, because i checked again today, and found they were both set to YES.
setting them both to NO appears to answer my question, my application is now working properly, and i will continue to test.
thank you, very much appreciated.

How to make a running application on macos to float above other apps?

I am making an application that can set a running application window to float above other apps so that if you click outside of the app's window, its window will still remain on the screen.
I have tried to use app.activate(options: NSApplication.ActivationOptions.activateIgnoringOtherApps) where app is the frontmost running application set by app = NSWorkspace.shared.frontmostApplication, but this method only shows the window and when you click outside the app it hides again.
func setAppFloating() {
let app = NSWorkspace.shared.frontmostApplication
app?.activate(options: NSApplication.ActivationOptions.activateIgnoringOtherApps)
}
I want to set the window of the running application to Floating so that when you click outside the window, the app should still remain visible above other apps.

After hiding NSRunningApplications some automatically unhide

Im using the following code to hide all the running applications whose activationPolicy == .regular and if the bundle identifier is different from my app.
NSWorkspace.shared.runningApplications
.filter { runningApps.contains($0.bundleIdentifier) }
.forEach { item in
item.hide()
}
Right after hiding them some just automatically unhide, apps that usually have a search bar or first responder textview like telegram, finder, messages. It's random and sometimes it reappears, sometimes not...
I also tried with NSWorkspace.shared.hideOtherApplications() but it does the same
macOS 10.14.3
Any idea on what should I do before hiding?

NSApplication keyWindow is nil during applicationDidFinishLaunching

Starting with a blank OS X application project, I add the following code to applicationDidFinishLaunching.
func applicationDidFinishLaunching(aNotification: NSNotification) {
let app = NSApplication.sharedApplication()
guard let window = app.keyWindow else {
fatalError("No keyWindow\n")
}
print(window)
}
At launch I hit the error case because my local window variable is nil. Yet when I show the contents of the app variable, I see a valid value for _keyWindow. Also notice that the blank GUI Window is being displayed on the screen next to the stack dump.
Why does the keyWindow: NSWindow? property return nil in this case?
Thanks
NSApplication's keyWindow property will be nil whenever the app is not active, since no window is focused for keyboard events. You can't rely on it being active when it finished launching because the user may have activated some other app between when they launched your and when it finished launching, and Cocoa is designed to not steal focus.
To some extent, you may be seeing that happen more when launching from Xcode because app activation is a bit strange in that case. But, still, you must not write your applicationDidFinishLaunching() method to assume that your app is active.
What you're seeing in terms of the app's _keyWindow instance variable is, of course, an implementation detail. One can't be certain about what it signifies and you definitely should not rely on it. However, I believe it's basically the app's "latent" key window. It's the window that will be made key again when the app is activated (unless it is activated by clicking on another of its windows).