Appdelegate#Applicationdidfinishlaunching Not Called for Swift 4 Macos App Built from Command Line

applicationDidFinishLaunching not invoked

In your MainMenu.xib, make sure there's an instance of your AppDelegate class. To make one, drag a plain object (blue cube) into the list and set its class name to AppDelegate (or whatever your app delegate class name is).

Also in the MainMenu.xib, to connect it, drag a connection from the Application object to your AppDelegate instance (the blue cube) and connect it to the delegate outlet.

Done.

NSApplicationDelegate's applicationDidFinishLaunching is not being called

If you want to implement Objective-C protocols in Swift 3, you need to use _ indicating the method has no label for the first parameter.

func applicationDidFinishLaunching(_ aNotification: Notification) {

(UPDATE)

Sorry, in my first code, I have forgotten to to replace NSNotification with Notification. (Thanks, Leo Dabus.)

Action of StatusItem not working in Swift

If the button is showing up and nothing is happening when you click it, it looks to me like you need to make sure you're setting your button's target to your App instance. E.g.:

button.target = self

Otherwise the action is only followed up the responder chain.

applicationDidFinishLaunching not running

Does your Application object have it's delegate set to your Application App Delegate object? Right click on Application in your IB window and set the delegate outlet to be your Application App Delegate object.

NSApplicationDelegate not working without Storyboard

You need to do a few things here

  1. Delete NSMainStoryboardFile key/value from the plist
  2. Create a NSApplication subclass and assign it to the Principal Class (NSPrincipalClass) key.

assign custom class to principal class

The name must be fully qualified with your module name.


  1. Manually instantiate your delegate in your NSApplication subclass and assign it to the delegate property.

Make sure you keep a strong reference to your delegate object. Ive just used a let here.

class GrookApplication: NSApplication {

let strongDelegate = AppDelegate()

override init() {
super.init()
self.delegate = strongDelegate
}

required init?(coder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}

}

e.g a simple delegate.

@NSApplicationMain
class AppDelegate: NSObject, NSApplicationDelegate {

override init() {
super.init()
print("wassup")
//conceptual proof of life init override
//wait until applicationDidFinishLaunching , specially for UI
}

var window: NSWindow!
func applicationDidFinishLaunching(_ aNotification: Notification) {
print("yo! I'm alive")
window = NSWindow(contentRect: NSRect(x: 0, y: 0, width: 200, height: 200), styleMask: .titled, backing: .buffered, defer: false)
window.makeKeyAndOrderFront(nil)
}

}

EDIT 2018 Verified High Sierra

Do NOT try and do window or view controller initialisation inside init this leads to double app initialisation issues and crashing. The app has not finished launching at this stage. Wait until applicationDidFinishLaunching to fire any significant operations.

Swift OSX what is the first method to be run?

You have 4 options (in order of appearance):

  • Override init()
  • Override awakeFromNib()
  • Delegate notification applicationWillFinishLaunching
  • Delegate notification applicationDidFinishLaunching

The best place depends on your needs.

  • If IBOutlets are involved put it not before awakeFromNib.
  • If you are using a view based table view don't put it in awakeFromNib because it could be called multiple times.
  • Registering NSUserDefaults can be put everywhere.
  • Consider also lazy initialization.
  • Consider also Cocoa Bindings.

Cocoa Mac : creating window from AppDelegate

You need to declare your NSWindowController variable main out of applicationDidFinishLaunching method. You need also to call makeKeyAndOrderFront(nil) instead of becomeFirstResponder:

import Cocoa

@NSApplicationMain
class AppDelegate: NSObject, NSApplicationDelegate {

var main: NSWindowController!

func applicationDidFinishLaunching(_ aNotification: Notification) {
// Insert code here to initialize your application

main = NSStoryboard(name : "Main", bundle: nil).instantiateController(withIdentifier: "MainWindow") as! NSWindowController
let mainVc = NSStoryboard(name:"Main", bundle: nil).instantiateController(withIdentifier: "MainViewController") as! ViewController
main.window?.contentViewController = mainVc
main.window?.makeKeyAndOrderFront(nil)

}
func applicationWillTerminate(_ aNotification: Notification) {
// Insert code here to tear down your application
}
}


Related Topics



Leave a reply



Submit