Current view controller from AppDelegate?
If you have UINavigationController into appDelegate then use its property topViewController or visibleViewController
Access current instance of view controller from app delegate in swift?
You have two options, the second one is better by design.
First option: (what you want)
I don't know the structure of your view controllers, so let me assume you have a root view controller, you could get it from AppDelegate
via:
rootVC = self.window?.rootViewController
And if you want to get the presented view controller from the root view controller (like many apps, the presented view controller is a tab bar controller):
guard let tabBarController = rootVC.presentedViewController as? TabBarController else {
return
}
Once you get your tab bar controller, you can find the view controller in the array of view controllers:
tabBarController.viewControllers
Essentially, what I'm trying to say is you have to jump through your view controllers starting from the root to get to the controller you want, then grab the variable from there. This is very error prone, and generally not recommended.
Second option (better practice):
Have your view controller register as an observer for the UIApplicationWillResignActiveNotification
notification. This will allow you to do whatever you want from the view controller when your app is about to enter background.
How to get the current visible viewController from AppDelegate
Do conditional casting on the return value to safely check its type.
if let currentVC = UIApplication.topViewController() as? MyViewController {
//the type of currentVC is MyViewController inside the if statement, use it as you want to
}
Your whole function implementation is flawed, if it actually worked, it would lead to infinite recursion. Once you find out the type of your current top view controller in your if statements, you are calling the same function again with the current root controller as its input value. Your function only ever exists, if it reaches either a call from a view controller, whose class is none of the ones specified in your optional bindings.
Moreover, your whole implementation doesn't do anything at the moment. You find out the type of your root view controller, but than you upcast it by returning a value of type UIViewController
.
Opening view controller from app delegate using swift
You have to set ViewController StoryBoardId property as below image.
open viewController using coding as below in swift
func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
let mainStoryboardIpad : UIStoryboard = UIStoryboard(name: "Main", bundle: nil)
let initialViewControlleripad : UIViewController = mainStoryboardIpad.instantiateViewControllerWithIdentifier("Circles") as UIViewController
self.window = UIWindow(frame: UIScreen.main.bounds)
self.window?.rootViewController = initialViewControlleripad
self.window?.makeKeyAndVisible()
return true
}
For iOS 13+ (based on an article by dev2qa)
Open SceneDelegate.swift
and add following
func scene(_ scene: UIScene, willConnectTo session: UISceneSession, options connectionOptions: UIScene.ConnectionOptions) {
// If this scene's self.window is nil then set a new UIWindow object to it.
self.window = self.window ?? UIWindow()
// Set this scene's window's background color.
self.window!.backgroundColor = UIColor.red
// Create a ViewController object and set it as the scene's window's root view controller.
self.window!.rootViewController = ViewController()
// Make this scene's window be visible.
self.window!.makeKeyAndVisible()
guard scene is UIWindowScene else { return }
}
There is an open-source navigation utility which attempts to make this easier. Example
How do I open a view controller from the app delegate?
You have 2 possible ways of doing this:
1. Instantiate your tab bar controller via storyboard.
Go to your storyboard and set a storyboard ID for your tab bar controller inside the identity inspector.
Once you go that, you can instantiate your view controller like this:
let storyboard = UIStoryboard(name: storyboardName, bundle: nil)
if let tabController = storyboard.instantiateViewController(withIdentifier: "tabControllerID") as? UITabController {
tabController.selectedIndex = index
}
- Or, you can access scene delegate from your AppDelegate and use window -> rootViewController to get access to your tabController:
let scene = UIApplication.shared.connectedScenes.first
if let sceneDelegate = scene?.delegate as? SceneDelegate {
if let tabController = sceneDelegate.window?.rootViewController as? UITabBarController {
tabController.selectedIndex = index
}
}
Present view controller from app delegate
You can also try:
[[[UIApplication sharedApplication] keyWindow] rootViewController]
How I use it:
#define ROOTVIEW [[[UIApplication sharedApplication] keyWindow] rootViewController]
[ROOTVIEW presentViewController:interstitialViewController animated:YES completion:^{}];
Related Topics
Count Unseen Messages with Firebase in Swift
How to Save an Array to .Plist in the App's Mainbundle in Swift
Div Scrolling Freezes Sometimes If I Use -Webkit-Overflow-Scrolling
Swift Uicolor Initializer - Compiler Error Only When Targeting Iphone5S
Updating to Latest Version of Cocoapods
How to Set Hex Color Code for Background
How to Delay a Method Call for 1 Second
Build Not Visible in Itunes Connect
How to Set the Uinavigationbar with Gradient Color
Query Available iOS Disk Space with Swift
How to Save Nsmutablearray or Nsdictionary Data as File in iOS
Phonegap 3.0 iOS Plugins Not Found
Extra Padding Above Table View Headers in iOS 15
How to Use Navigation Controller Inside of Uitabbarcontroller with Storyboard on Swift
Set a Maximum Number of Children in Firebase
How to Load a Url Link That Is Inside a Web View and Keep It in That Web View in Swift
Wkwebview Fails to Load Images and CSS Using Loadhtmlstring(_, Baseurl:)