Marmelador
Marmelador

Reputation: 1017

AppDelegate.swift and Main.swift and how to use them

I have learned a fair bit of the basics of Swift in the playground. This question is still pretty basic so bear with me. It boils down to this:

Where do the top-level expressions go? And what is the best etiquette here? Say I wanted to make an app that just print "Hello world" to the console. Where do I put the print("Hello World") function?

The AppDelegate with its @UIApplicationMain attribute seems to have that job. But I only ever have gotten top-level expressions to work by removing said attribute, creating a main.swift file and putting it there.

I am trying to do this with an iOS-App

Upvotes: 1

Views: 3080

Answers (1)

Developer Omari
Developer Omari

Reputation: 434

please check the iOS application lifecycle AppCycle. The didFinishLaunchingWithOptions method is executed when the your app is launched. I would recommend you to put your code inside that function.

       func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {

Its the starting point where you actually create your mainWindow and rootViewController but you can also do other execution and put your print statements inside that function! If you are using storyboards this is done automatically for you. and you put your code inside your ViewController Class that is connected to your storyboard

Here is the application lifecycle :

enter image description here

App-States

Not running The app has not been launched or was running but was terminated by the system.

Inactive The app is running in the foreground but is currently not receiving events. (It may be executing other code though.) An app usually stays in this state only briefly as it transitions to a different state.

Active The app is running in the foreground and is receiving events. This is the normal mode for foreground apps.

Background The app is in the background and executing code. Most apps enter this state briefly on their way to being suspended. However, an app that requests extra execution time may remain in this state for a period of time. In addition, an app being launched directly into the background enters this state instead of the inactive state. For information about how to execute code while in the background, see Background Execution.

Suspended The app is in the background but is not executing code. The system moves apps to this state automatically and does not notify them before doing so. While suspended, an app remains in memory but does not execute any code. When a low-memory condition occurs, the system may purge suspended apps without notice to make more space for the foreground app.

Here are the other state transitions you find them in your Appdelegate class

application:willFinishLaunchingWithOptions:—This method is your app’s first chance to execute code at launch time.

application:didFinishLaunchingWithOptions:—This method allows you to perform any final initialization before your app is displayed to the user.

applicationDidBecomeActive:—Lets your app know that it is about to become the foreground app. Use this method for any last minute preparation.

applicationWillResignActive:—Lets you know that your app is transitioning away from being the foreground app. Use this method to put your app into a quiescent state.

applicationDidEnterBackground:—Lets you know that your app is now running in the background and may be suspended at any time.

applicationWillEnterForeground:—Lets you know that your app is moving out of the background and back into the foreground, but that it is not yet active.

applicationWillTerminate:—Lets you know that your app is being terminated. This method is not called if your app is suspended.

(Description of lifecycle and transitions taken from apple website)

Upvotes: 1

Related Questions