Setting root view controller upon launch, depending on user auth state - ios

This is the storyboard layout I have right now
So what I need is to check if the user is already authenticated when the app launches.
If he is not logged in set the root view controller to the navigation controller with the login and register forms.
If he is logged in set it to the tab bar view controller.
I tried a lot of different solutions but none of them worked. It just kept setting the view controller to the one marked with "Is Initial View Controller".
This is the code I tried in the AppDelegate:
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
FirebaseApp.configure()
self.window = UIWindow(frame: UIScreen.main.bounds)
let storyboard = UIStoryboard.init(name: "Main", bundle: nil)
var viewController: UIViewController
if isAuthenticated() {
viewController = storyboard.instantiateViewController(withIdentifier: Constants.Storyboards.homeViewController) as! UITabBarController
} else {
viewController = storyboard.instantiateViewController(withIdentifier: Constants.Storyboards.authViewController) as! UINavigationController
}
self.window?.rootViewController = viewController
self.window?.makeKeyAndVisible()
return true
}
I can do this by pressing a button easily, but I want it to happen without the user needing to do something.
EDIT: Thanks to #LukaCefarin and #Francesco Deliro I managed to find out that the problem was. I was using XCode 11 and the rootViewController had to be set in the SceneDelegate.swift
This is what my code in the SceneDelegate.swift looks like for anyone who has a similar issue:
func scene(_ scene: UIScene, willConnectTo session: UISceneSession, options connectionOptions: UIScene.ConnectionOptions) {
guard let _ = (scene as? UIWindowScene) else { return }
self.window = UIWindow(windowScene: scene as! UIWindowScene)
let storyboard = UIStoryboard.init(name: "Main", bundle: nil)
var viewController: UIViewController;
if isAuthenticated() {
viewController = storyboard.instantiateViewController(withIdentifier: "HomeVC")
} else {
viewController = storyboard.instantiateViewController(withIdentifier: "AuthVC")
}
self.window?.rootViewController = viewController
self.window?.makeKeyAndVisible()
}

EDIT:
This solution and setting the window rootViewController in the AppDelegate work for versions prior to Xcode11 and iOS13. As suggested in the comments by Luka Cefarin, if you are using Xcode11 and iOS13 you have to set the window rootViewController in the SceneDelegate.swift file.
You have to remove the Main Interface and to uncheck the initial view controller in the storyboard:
Before
After

Related

Programmatically embedding in navigation controller (Xcode 14.2, io2 16+)

I had a working program consisting basically of a table view controller embedded in a navigation controller, and I decided to try to get rid to some mysterious/obnoxious warnings that first appeared in iOS 16 ("UINavigationBar decoded as unlocked for UINavigationController, or navigationBar delegate set up incorrectly"). Following online suggestions, I got rid of the navigation controller on the storyboard, and added some code to
func scene(_ scene: UIScene, willConnectTo session: UISceneSession, options connectionOptions: UIScene.ConnectionOptions) {
guard let windowScene = (scene as? UIWindowScene) else { return }
window?.windowScene = windowScene
window?.makeKeyAndVisible()
// ViewController: UITableViewController is my VC class
let viewController = ViewController()
let navViewController = UINavigationController(rootViewController: viewController)
window?.rootViewController = navViewController
}
to embed it programmatically. The program would then crash with a message that stating that it could not deque a cell with identifier "itemCell", and "must register a nib or a class for the identifier or connect a prototype cell in a storyboard". I solved that by adding the line
self.tableView.register(UITableViewCell.self, forCellReuseIdentifier: "itemCell")
in ViewDidLoad but I don't understand why this was necessary, since the identifier for the prototype cell is still there in the storyboard, just as it was before. Can someone illuminate me? Thanks.
This:
let viewController = ViewController()
let navViewController = UINavigationController(rootViewController: viewController)
window?.rootViewController = navViewController
won't give you what you want.
In Storyboard, you have to give ViewController a Storyboard ID (such as "FirstViewController"), then instantiate it via code:
let storyboard = UIStoryboard(name: "Main", bundle: nil)
let initialViewController = storyboard.instantiateViewController(withIdentifier: "FirstViewController")
let navViewController = UINavigationController(rootViewController: initialViewController)
Edit
Assuming the view controller we ID as "FirstViewController" in Storyboard is a table view controller, and we want to load it as the "root" controller for a navigation controller, the full code block would be:
func scene(_ scene: UIScene, willConnectTo session: UISceneSession, options connectionOptions: UIScene.ConnectionOptions) {
guard let windowScene = (scene as? UIWindowScene) else { return }
window?.windowScene = windowScene
let storyboard = UIStoryboard(name: "Main", bundle: nil)
let initialViewController = storyboard.instantiateViewController(withIdentifier: "FirstViewController")
let navViewController = UINavigationController(rootViewController: initialViewController)
window?.rootViewController = navViewController
window?.makeKeyAndVisible()
}

how to use navigation controller programaticaly?

i have a button in viewcontroller which navigates to editPage viewcontrooler
i want to use pushNavigation
i did below code in button action method
#IBAction func editBtn(_ sender: Any) {
let storyBoard: UIStoryboard = UIStoryboard(name: "Main", bundle: nil)
let newViewController = storyBoard.instantiateViewController(withIdentifier: "editProfile") as! editProfile
let navigation = UINavigationController(rootViewController: newViewController)
self.navigationController?.pushViewController(navigation, animated: true)
}
but when i click on button it dosent navigate
i dont have navigation controler in storyboard.......
i dont want to present view controler
Looks like you are setting your viewController as initail View Controller from storyboard but do not embed this in navigation controller , either embed your viewController in navigationController and set this navigation controller as initialViewController or follow this approach as you don't want to do it from storyboard:
In SceneDelegate , write :
var window: UIWindow?
and then in this method :
func scene(_ scene: UIScene, willConnectTo session: UISceneSession, options connectionOptions: UIScene.ConnectionOptions) {
// Use this method to optionally configure and attach the UIWindow `window` to the provided UIWindowScene `scene`.
// If using a storyboard, the `window` property will automatically be initialized and attached to the scene.
// This delegate does not imply the connecting scene or session are new (see `application:configurationForConnectingSceneSession` instead).
guard let _ = (scene as? UIWindowScene) else { return }
//Setup user state
if let vc = UIStoryboard.init(name: "YOUR_STORYBOARD_NAME", bundle: nil).instantiateViewController(withIdentifier: "VIEW_CONTROLLER_IDENTIFIER") as? ViewController {
let nav = UINavigationController(rootViewController: vc)
window?.rootViewController = nav
window?.makeKeyAndVisible()
}
}
for IOS < 13, in AppDelegate.swift :
var window : UIWindow?
//MARK:- DID FINISH LAUNCHING WITH OPTIONS : CALLED EVERYTIME WHEN APPLICATION LAUNCHES
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
if let vc = UIStoryboard.init(name: "YOUR_STORYBOARD_NAME", bundle: nil).instantiateViewController(withIdentifier: "VIEW_CONTROLLER_IDENTIFIER") as? ViewController {
let nav = UINavigationController(rootViewController: vc)
window?.rootViewController = nav
window?.makeKeyAndVisible()
}
return true
}

Swift: Two View Controllers presented

Apologies for the ambiguity of the question; I'll try to explain clearer here.
The Problem
I have some code in AppDelegate.swift that performs conditional navigation; if the user is signed in (and therefore currentUser is not nil), the app will start in View, a View Controller in the Main storyboard. Else, if the user isn't signed in, the app will start in Welcome, a View Controller in an Auth storyboard.
The following is my code from AppDelegate.swift, ViewController.swift (which is View), and WelcomeViewController.
Note:
View is the Storyboard ID I set for ViewController; Welcome is the Storyboard ID I set for WelcomeViewController.
AppDelegate.swift
import UIKit
import Firebase
#UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate {
var window: UIWindow?
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
// Configures the Firebase library.
FirebaseApp.configure()
// Performs conditional navigation on the condition of currentUser's status.
self.window = UIWindow(frame: UIScreen.main.bounds)
let user = Auth.auth().currentUser
if user != nil {
let storyboard = UIStoryboard(name: "Main", bundle: nil)
let rootViewController = storyboard.instantiateViewController(identifier: "View")
window?.rootViewController = rootViewController
UIView.animate(withDuration: 0.5) {
self.window?.makeKeyAndVisible()
}
} else {
let storyboard = UIStoryboard(name: "Auth", bundle: nil)
let rootViewController = storyboard.instantiateViewController(identifier: "Welcome")
window?.rootViewController = rootViewController
UIView.animate(withDuration: 0.5) {
self.window?.makeKeyAndVisible()
}
}
return true
}
}
ViewController.swift
import UIKit
class ViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view.
print("ViewController presented.")
}
}
WelcomeViewController.swift
import UIKit
class WelcomeViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view.
print("WelcomeViewController presented.")
}
}
Expectations
The app is freshly installed on the Simulator, and therefore there should be no currentUser (and upon debugging, proves to be true). Therefore, I'd expect to see this as the output in the Console:
WelcomeViewController presented.
Instead, this shows up in the Console:
WelcomeViewController presented.
ViewController presented.
Attempts
So far, I thought that the problem might have arised from the fact that Main storyboard is the default Storyboard set since the creation of the project. Therefore, I tried unchecking the 'Initial View Controller' checkbox for the two View Controllers. Just like I'd expected, the following appears instead:
WelcomeViewController presented.
2020-06-20 11:39:38.724658+0800 AppName[11439:237509] [WindowScene] Failed to instantiate the default view controller for UIMainStoryboardFile 'Main' - perhaps the designated entry point is not set?
What should I do to make sure that ViewController doesn't appear and replace WelcomeViewController? All help is appreciated, thanks!
why not set WelcomeViewController as your 'Initial View Controller' and replace its viewDidLoad code with AppDelegate file then:
class WelcomeViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view.
print("WelcomeViewController presented.")
let user = Auth.auth().currentUser
if user != nil {
let storyboard = UIStoryboard(name: "Main", bundle: nil)
let rootViewController = storyboard.instantiateViewController(identifier: "View")
window?.rootViewController = rootViewController
UIView.animate(withDuration: 0.5) {
self.window?.makeKeyAndVisible()
}
}
}
}
you can also look this if you want to set the view controller from AppDelegate:
EDIT:
you can use a UINavigationController as your initial view controller as indicated in this answer:
set initial viewcontroller in appdelegate - swift
then you just need to set the correct view controller here:
navigationController.viewControllers = [rootViewController]
self.window?.rootViewController = navigationController
Solution
I managed to find the solution to the issue that I'm facing here. There are two mistakes above in the code, and I'll explain it here:
Mistake 1: Code should be in SceneDelegate instead of AppDelegate (iOS 13)
Xcode 11, along with the release of iOS 13, introduced SceneDelegate.swift. I was supposed to put my code above in SceneDelegate.swift instead of AppDelegate.swift; because I didn't do this, the code hadn't worked as I'd hoped for my Simulator, which was running iOS 13.5.
Here's my rectified code in SceneDelegate.swift:
import UIKit
import Firebase
class SceneDelegate: UIResponder, UIWindowSceneDelegate {
var window: UIWindow?
func scene(_ scene: UIScene, willConnectTo session: UISceneSession, options connectionOptions: UIScene.ConnectionOptions) {
// Use this method to optionally configure and attach the UIWindow `window` to the provided UIWindowScene `scene`.
// If using a storyboard, the `window` property will automatically be initialized and attached to the scene.
// This delegate does not imply the connecting scene or session are new (see `application:configurationForConnectingSceneSession` instead).
guard let _ = (scene as? UIWindowScene) else { return }
// Performs conditional navigation on the condition of currentUser's status.
let user = Auth.auth().currentUser
if user != nil {
let storyboard = UIStoryboard(name: "Main", bundle: nil)
let rootViewController = storyboard.instantiateViewController(identifier: "View")
window?.rootViewController = rootViewController
UIView.animate(withDuration: 0.5) {
self.window?.makeKeyAndVisible()
}
} else {
let storyboard = UIStoryboard(name: "Auth", bundle: nil)
let rootViewController = storyboard.instantiateViewController(identifier: "Welcome")
window?.rootViewController = rootViewController
UIView.animate(withDuration: 0.5) {
self.window?.makeKeyAndVisible()
}
}
}
}
Mistake 2 (Important!): Definition of new UIWindow
In my code above, there was a faulty line which caused the creation of a new UIWindow:
self.window = UIWindow(frame: UIScreen.main.bounds)
By removing this line of code, the problem was resolved. Here's the rectified code:
let user = Auth.auth().currentUser
if user != nil {
let storyboard = UIStoryboard(name: "Main", bundle: nil)
let rootViewController = storyboard.instantiateViewController(identifier: "View")
window?.rootViewController = rootViewController
UIView.animate(withDuration: 0.5) {
self.window?.makeKeyAndVisible()
}
} else {
let storyboard = UIStoryboard(name: "Auth", bundle: nil)
let rootViewController = storyboard.instantiateViewController(identifier: "Welcome")
window?.rootViewController = rootViewController
UIView.animate(withDuration: 0.5) {
self.window?.makeKeyAndVisible()
}
}
I hope this helps anyone else facing this issue!

Loading screen off centre based on launch storyboard?

I have 2 storyboards: Onboarding and Main. Everything works great as far as execution goes. The issue is that I have loading screen at the start of Main.storyboard that looks different depending on which storyboard launches.
Scenario 1: New user launches the app, it goes through Onboarding.storyboard then Main.storyboard launches and the loading screen looks as it should like this: Correct Screen
Scenario 2: User launches the app for the second time, therefore Onboarding.storyboard is not launched: Loading screen is off centre like this: Wrong Screen
Long story short: If there is no Onboarding.storyboard, it doesn't look right. The issue is only shown on iPad landscape; portrait looks fine.
All of my loading screen code is inside my first view controller of Main.Storyboard so I'm guessing that the issue is the order in which the code is called?
Onboarding.Storyboard only has 1 view controller
Main.Storyboard goes: TabBarController -> NavController -> FirstVC
I placed my SceneDelegate code below along with the loading screen code of FirstVC.
In case you want to dig in deeper, I'm using RevealingSplashView available on github. Since I don't think that's the issue I didn't get into that further.
SceneDelegate:
func scene(_ scene: UIScene, willConnectTo session: UISceneSession, options connectionOptions: UIScene.ConnectionOptions) {
guard let windowScene = (scene as? UIWindowScene) else { return }
window = UIWindow(frame: windowScene.coordinateSpace.bounds)
window?.windowScene = windowScene
let onboardingStoryboard = UIStoryboard(name: "Onboarding", bundle: nil)
let mainStoryboard = UIStoryboard(name: "Main", bundle: nil)
var vc: UIViewController
if UserDefaults.standard.value(forKey: "firstTimer") == nil {
vc = onboardingStoryboard.instantiateInitialViewController()!
} else {
vc = mainStoryboard.instantiateInitialViewController()!
}
self.window?.rootViewController = vc
self.window?.makeKeyAndVisible()
}
FirstVC relevant code:
class firstVC: UIViewController {
var revealingSplashView : RevealingSplashView!
override func viewDidLoad() {
super.viewDidLoad()
showLoadingScreen()
}
func showLoadingScreen() {
revealingSplashView = RevealingSplashView(iconImage: UIImage(named: "Icon")!, iconInitialSize: CGSize(width: 150, height: 150), backgroundImage: UIImage(named: "loadBackground")!)
revealingSplashView.animationType = .heartBeat
revealingSplashView.startAnimation()
view.addSubview(revealingSplashView)
}
Check to see that your layout constraints are the same in both of your storyboards. I'd recommend just setting them to center horizontal and center vertical on both of them.
In your scene delegate callback, why are you calling instantiateInitialViewController() twice per storyboard?
In my iOS 12 project, I used storyboards to kick things off. In app delegate didFinishLaunching, I would override and set the window if and only if it was a new user. So my "Main" storyboard would kick in most of the time.
Same technique could be applied here. Check out the Info.plist values you need to set for UIScene storyboard to work. You can override the window if you want to show the first time onboarding, otherwise let the system instantiate your main storyboard.
Further testing revealed that if I return SceneDelegate to its "factory default" state, therefore launching Main.storyboard, I had no issues.
To fix the issue I simply changed my SceneDelegate to:
func scene(_ scene: UIScene, willConnectTo session: UISceneSession, options connectionOptions: UIScene.ConnectionOptions) {
guard let windowScene = (scene as? UIWindowScene) else { return }
if UserDefaults.standard.value(forKey: "firstTimer") == nil {
window = UIWindow(frame: windowScene.coordinateSpace.bounds)
window?.windowScene = windowScene
let onboardingStoryboard = UIStoryboard(name: "Onboarding", bundle: nil)
var vc: UIViewController
vc = onboardingStoryboard.instantiateInitialViewController()!
self.window?.rootViewController = vc
self.window?.makeKeyAndVisible()
}
}

Second start of application only display a black view

On firstStart of the application I want the user to start at ViewController1 (VC1) and after completing the onboarding process and a button gets clicked in ViewController3 (VC3) should the first item of the Tab Bar controller be the new initial view controller.
The first launch works but when closing the app and upon reopening the app everything goes black.
When I set VC3 as the new initial viewController everything works fine.
My SceneDelegate
var window: UIWindow?
func scene(_ scene: UIScene, willConnectTo session: UISceneSession, options connectionOptions: UIScene.ConnectionOptions) {
guard let windowScene = (scene as? UIWindowScene) else { return }
self.window = UIWindow(windowScene: windowScene)
//self.window = UIWindow(frame: UIScreen.main.bounds)
// Is it the apps first start?
let firstView = UserDefaults.standard.string(forKey: "firstStart")
let storyboard = UIStoryboard(name: "Main", bundle: nil)
// Create the root view controller as needed
guard let vc = storyboard.instantiateViewController(identifier: firstView ?? "Welcome") as? ViewController else {
print("ViewController not found")
return
}
let nc = UINavigationController(rootViewController: vc)
// Create the window.
let win = UIWindow(windowScene: windowScene)
win.rootViewController = nc
win.makeKeyAndVisible()
window = win
}
My button:
#IBAction func buttonActivate(_ sender: Any) {
// Change start View to "Home"
UserDefaults.standard.set("Home", forKey: "firstStart")
}
I've tried with changing keys in Info.plist and read through UIApplicationSceneManifest but without any success. Tried having an additional navigation controller as tab bar controller item 2 shows but same result with a black screen.
You shouldn't try and change the window, instead change the .rootViewController.
VC1 is the entry point in storyboard which means that the initial view controller your app starts with every time, during the "didFinishLaunchingWithOptions" in app-delegate you can ask whether the user has been promoted with the onboarding and in that case switch the rootViewController as follows:
func showMainStoryboard() {
let homeViewController = UIStoryboard(name: "StoryboardName", bundle: Bundle.main).instantiateInitialViewController()
window?.rootViewController = homeViewController
}
In order for the following example to work for you, you must create a separate storyboard: 1 for the onboarding and 1 for main.
Flow #1:
The first time user installs the app he is promoted with the onboarding VC after he clicked the button on VC3 you switch the rootViewController on that event.
Flow #2:
After the user clicked the button and reentered the app he won't be promoted with the onboarding but routed to the "main" VC, a.k.a Start in your case.

Resources