DidFinishLaunchingWithOptions private function not called? (Swift 3)

Is didFinishLaunchingWithOptions when the application is launched for the first time? I set a breakpoint with this method, and when I run the application in the simulator, the breakpoint does not hit, which means that the method is not called. I try to load some data from UserDefaults whenever the application starts, but it is completely ignored. One thing that I noticed is that it defaults to private func instead of func . If I get rid of private , I get a warning that "there is an almost similar optional requirement in UIApplicationDelegate." Can someone explain to me what this means, and does private func have anything to do with the ignored method? Is this method even supposed to be called when my application runs in the simulator? If not, how can I check if data is retrieved after starting my application? All other methods in AppDelegate actually called normally (for example, the applicationDidEnterBackground method works fine).

+9
source share
5 answers

Uninstall your method signature and have Xcode fill it in automatically

I also had a problem with my didFinishLaunchingWithOptions method in AppDelegate not being called. My function was also marked as private and looked like this

 private func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool 

The problem is that this is an old syntax! Obviously for me, when I converted my project from Swift 2.x to Swift 3, Xcode did not convert the methods to AppDelegate. The new syntax is as follows

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

Swift 4.2:

 func application( _ application: UIApplication,didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool 
+33
source

for Swift ~ 3.0 Replace didFinishLaunchingWithOptions with signature signature

 func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey : Any]? = nil) -> Bool { } 
+3
source

Have you implemented didFinishLaunchingWithOptions in one of your ViewControllers ? One of them will not receive a call for a custom implementation of this method. This method is defined in ApplicationDelegate , and it will always be called after the application starts. If you have not defined a method in any ViewController , and one in AppDelegate not called, try resetting the simulator. From the simulator menu Simulator -> Reset content and settings .

If the compiler requests the didFinishLaunchingWithOptions private method, then the method parameter may cause an error. The application delegation method parameter (_: didFinishLaunchingWithOptions :) is now connected to Swift as [UIApplicationLaunchOptionsKey: Any]?, Rather than [NSObject: AnyObject] ?. Therefore, change the method signature as shown.

 func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool { // ... } 
+2
source

This method is defined in ApplicationDelegate, and it will always be called after the application starts. If you have not yet defined a method in any ViewController, and one that is not called in AppDelegate, try resetting the simulator.

Open the simulator β†’ menu Simulator β†’ Reset contents and settings.

 -(BOOL)application(UIApplication*)applicationdidFinishLaunchingWithOptions:(NSDictionary *)launchOptions { //.. } 
+1
source

Update for Swift 4.2:

 func application( _ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]? ) -> Bool 
+1
source

Source: https://habr.com/ru/post/1257772/


All Articles