0

I implemented the NSApplicationDelegate method func application(_ sender: NSApplication, openFile filename: String) -> Bool to get the name of the file that is double-clicked to launch my macOS app (Swift 4.x). However, it appears that this method is called after ViewController viewDidLoad() which is where all my initialization code takes place. Is there a way to get the file name in viewDidLoad() in the ViewController class so that I can utilize the file name directly in my initialization code?

Update:

I am now able to pass data from AppDelegate to ViewController based on the suggestion from this link for Swift 4.2:

https://stackoverflow.com/questions/15049924/passing-data-from-app-delegate-to-view-controllerhttps://stackoverflow.com/questions/15049924/passing-data-from-app-delegate-to-view-controller

So my question remains is how to get the file name either directly in ViewDidLoad() or be able to get the name throughapplication(_, sender: NSApplication, openFile filename: String) -> Bool and have it available to be passed into the ViewController when ViewDidLoad() is called.

Kenny
  • 429
  • 6
  • 22
  • Your viewController has a magical property called representedObject (MVC programming pattern). Make your code dependent on settter of this property (refresh UI etc.) and don't forget to use it when you pass data to your viewcontroller (ps: you can use own property if you don't like this one) <- This is prefer solution. Second solution is to showWindow in applicationDidFinishLaunching: You instantiate storyboard/XIB manually and show window instead of Storyboard. Don't forget to remove from storyboard (window-> Show at launch). – Marek H Mar 13 '19 at 17:50
  • Also make sure your viewcontroller doesn't know what is the filename. That logic should be somewhere else. Your NSViewController is just about to show objects on the screen. Otherwise you will have MVC (massive view controller) – Marek H Mar 13 '19 at 17:58
  • What should happen if you double-click another file while the app is running? – Willeke Mar 13 '19 at 22:33
  • Ideally, I would like to launch a separate process of the app with that other file. – Kenny Mar 13 '19 at 22:51
  • separate process -> you should use document based app instead. It is nicely documented how/when document is loaded with plenty of examples. Otherwise you have to manually do a lot of code. – Marek H Mar 14 '19 at 08:23

1 Answers1

0

Solved my problem. Instead of performing my initialization code in ViewController, I implemented the code in AppDelegate applicationDidFinishLaunching instead so that the initialization can be performed when the app is launched and before first event occurs. I believe this is more proper practice anyway for AppDelegate to handle app initialization. This code adjustment solves both the need to pass data between ViewController and AppDelegate, as well as the issue that application(_, sender: NSApplication, openFile filename: String) -> Bool is called after ViewDidLoad().

Kenny
  • 429
  • 6
  • 22