Updated 31 December 2020
App delegate object manages your app’s shared behaviors. This is your application root object.
it works in conjunction with UIApplication
to manage some interactions with the system.
App delegate object created when the app launched and object present in the whole application.
So we can use the app delegate object to fetch the UIApplication shared object.
App delegate provides help to know the app’s current state.
iOS Application Life Cycle depends on app delegate functions.
iOS Application Life Cycle delegate provides control of the application.
When we using core data then we should create a managed object of core data in the App delegate page.
And we can fetch managed objects in any view controller using the below code.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
if let delegate = UIApplication.shared.delegate as? AppDelegate { let moc = delegate.managedObjectContext // your code here } Here's an extension for UIApplicationDelegate that avoids hardcoding the AppDelegate class name: extension UIApplicationDelegate { static var shared: Self { return UIApplication.shared.delegate! as! Self } } // use like this: let appDelegate = MyAppDelegate.shared // will be of type MyAppDelegate |
App Delegate provides some methods to manage the app life cycle.
These methods called according to state.
These methods invoke automatically.
And Always try to create these methods by self do not copy these delegate from any place because sometimes these functions not worked.
1-> application:willFinishLaunchingWithOptions:
This method called when the first time your app launched and here you can get your launch-related data.
2-> application:didFinishLaunchingWithOptions:
This allows you to perform any final initialization before your app is displayed to the user.
3-> applicationDidBecomeActive
This method work when the app comes in the foreground state.
4-> applicationDidEnterBackground:
This method work when your app is now running in the background and maybe suspended at any time.
5-> applicationWillEnterForeground:
This helps when App moving from background to foreground but the app not in the active state.
6-> applicationWillTerminate:
This method called when the app being terminated and called when the app suspended.
And thanks for reading this blog, You can get other blogs from here
This will help you to use the App Delegate function and if you feel any issue or suggestion then leave a comment.
If you have more details or questions, you can reply to the received confirmation email.
Back to Home
Be the first to comment.