Private func didFinishLaunchingWithOptions not being called? (Swift 3)
Asked Answered
S

6

11

Isn't didFinishLaunchingWithOptions supposed to be called when the app starts running for the first time? I set a breakpoint at this method and when I run the app in the simulator the breakpoint doesn't get hit, which means the method doesn't get called. I'm trying to load some data from UserDefaults whenever the app launches, but it's being completely ignored. One thing I noticed is that it's by default a private func instead of a func. If I get rid of the private, I receive a warning that "there's an almost similar optional requirement in the UIApplicationDelegate". Can someone explain to me what this means and whether or not the private func has anything to do with the method being ignored? Is that method even supposed to be called when I run my app in the simulator? If not, how can I test if data is being retrieved after my app launches? All the other methods in the AppDelegate do get called normally (for example, the applicationDidEnterBackground method works perfectly fine).

Subcortex answered 6/10, 2016 at 6:2 Comment(0)
F
40

Remove your method signature and have Xcode autocomplete it

I also had the problem that my didFinishLaunchingWithOptions method in AppDelegate would not be called. My function was also marked private and looked like this

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

The problem is that this is the old syntax! Apparently for me when I converted my project from Swift 2.x to Swift 3 Xcode did not convert the methods in AppDelegate. The new syntax looks like this

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

Swift 4.2:

func application( _ application: UIApplication,didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool
Frick answered 31/10, 2016 at 9:45 Comment(5)
Sorry I actually fixed this issue a while back. Forgot to mark this question as answered. To fix this problem, I just had to replace AnyObject with Any as the value of the dictionary parameter of this method. It's really weird that you can still call AnyObject in Swift 3 even though that class has been renamed to Any and is fully deprecated. But there's a few other example of those type of classes as well, AnyObject isn't the only one.Subcortex
That's exactly what I was trying to say. Method signature changed from AnyObject to Any so your method with AnyObject was not recognized as the 'didFinishLaunching' methodFrick
It's pretty crazy that the conversion fails to convert this function and results in it not being called....Springhouse
@Frick AnyObject hasn't been renamed to Any, they both existed already. The difference is that AnyObject refers only to objects, and Any can refer to anything, including objects, structs, enums, etc. Also, it's worth mentioning that AnyObject is not a class, but a protocol. Although the conversion shouldn't be failing, the fact that it's not being called is normal. Swift encodes the types of each function, both the arguments and return values, into the binary's symbols, which allows it to do function overloading. It's actually a good thing, usually. ;)Initiate
Why on earth Xcode doesn't complain about outdated signatures is beyond me.Provitamin
W
3

for Swift ~3.0 Replace didFinishLaunchingWithOptions with follwing signature

func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey : Any]? = nil) -> Bool {
}
Weighin answered 2/8, 2017 at 11:23 Comment(0)
K
2

Have you implemented the didFinishLaunchingWithOptions in one of your ViewControllers? One will not get a call to the custom implementation of this method. This method is defined in the ApplicationDelegate and it will always be called once the app is launched. If you haven't defined the method again in any ViewController and the one in AppDelegate is not being called, then try resetting the simulator. From the simulator menu Simulator -> Reset content and settings.

If compiler prompts to make the didFinishLaunchingWithOptions method private then the parameter of the method might be causing the error. The parameter of the application(_:didFinishLaunchingWithOptions:) delegate method is now bridged to Swift as a [UIApplicationLaunchOptionsKey: Any]?, rather than an [NSObject : AnyObject]?. So modify the method signature as shown.

func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
// ...
}
Ketron answered 6/10, 2016 at 6:20 Comment(1)
Tried resetting content and settings, didn't work. And no, I didn't implement the method anywhere else, I know that's useless, since it's a delegate method. I'm still assuming it has something to do with the warning I get when I get rid of the private in front of the method call. Or at least it could be the private statement which causes the method to be ignored at runtime?Subcortex
H
1

This method is defined in the ApplicationDelegate and it will always be called once the app is launched. If you have not defined the method again in any ViewController and the one in AppDelegate is not being called, then try resetting the simulator.

Open simulator - > menu Simulator -> Reset content and settings.

-(BOOL)application(UIApplication*)applicationdidFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
//..
}
Hostility answered 6/10, 2016 at 7:35 Comment(0)
Y
1

Update for Swift 4.2:

func application(
    _ application: UIApplication, 
    didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?
) -> Bool
Yolondayon answered 30/12, 2018 at 5:1 Comment(0)
B
0

Delete app from device and restart Xcode worked for me here

Bettyannbettye answered 26/10, 2019 at 19:47 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.