iOS - How to remove UINavigationController from AppDelegate? [duplicate] - swift

This question already has answers here:
Removing NavigationController Programmatically
(2 answers)
Closed 4 years ago.
I have to remove UINavigationController from my app to add UITabBarController and keep
my if statement works as is.
My Code :
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
let storyboard = UIStoryboard(name: "Main", bundle: nil)
if UserDefaults.standard.value(forKey: "URL") == nil
{
let viewController = storyboard.instantiateViewController(withIdentifier: "ViewController") as! ViewController
let navController = UINavigationController(rootViewController: viewController)
self.window?.rootViewController = navController
self.window?.makeKeyAndVisible()
}else{
let viewController = storyboard.instantiateViewController(withIdentifier: "ChannelsViewController") as! ChannelsViewController
let navController = UINavigationController(rootViewController: viewController)
self.window?.rootViewController = navController
self.window?.makeKeyAndVisible()
}
return true
}

func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
let storyboard = UIStoryboard(name: "Main", bundle: nil)
if UserDefaults .standard .value(forKey: "URL") == nil
{
let viewController = storyboard.instantiateViewController(withIdentifier: "ViewController") as! ViewController
self.window?.rootViewController = viewController
self.window?.makeKeyAndVisible()
}else{
let viewController = storyboard.instantiateViewController(withIdentifier: "ChannelsViewController") as! ChannelsViewController
self.window?.rootViewController = viewController
self.window?.makeKeyAndVisible()
}
return true
}

Related

How to navigate to a UIViewController programmatically?

I am having difficulties navigating to a UIViewController programmatically from within another UIViewController. I am using UIKit and Swift 5.
I have tried to follow this Medium article but can't get it to work for me.
In my AppDelegate file I have changed the didFinishLaunchingWithOptions function to this:
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
self.window = UIWindow(frame: UIScreen.main.bounds)
self.window?.rootViewController = LoginViewController()
self.window?.makeKeyAndVisible()
return true
}
And then in my LoginViewController() I attempt to navigate to my DashboardViewController by doing the following:
override func viewDidLoad() {
super.viewDidLoad();
// present a modal with an embed UINavigationController
let rvc = DashboardViewController()
let vc = UINavigationController(rootViewController: rvc)
vc.modalPresentationStyle = .overFullScreen
present(vc, animated: true, completion: nil)
}
But DashboardViewController is never called.
What am I doing wrong and how can I change the code to work?
Thank you.
Try this in your viewDidLoad method:
let rvc = self.storyboard!.instantiateViewControllerWithIdentifier("DashboardViewController") as! DashboardViewControllerSwift
let navController = UINavigationController(rootViewController: rvc)
navController.modalPresentationStyle = .overFullScreen
self.present(navController, animated:true, completion: nil)
Change DashboardViewControllerSwift to the name of your Swift file for your DashboardViewController, without the .swift bit. Make sure it is set as the class for DashboardViewController object in IB/Storyboard.
Also ensure the DashboardViewController object in IB/Storyboard has its Storyboard Identifier set as DashboardViewController.
1- start with SceneDelegate
self.window = UIWindow(windowScene: windowScene)
let navigation = UINavigationController()
window.rootViewController = navigation
window?.makeKeyAndVisible()
2-then
let rvc = DashboardViewController()
self.navigationController?.pushViewController(rvc, animated: true)
3- try it in viewDidLoad() if not work try it in viewWillAppear
I ended up coming across a Youtube video that helped me solve my issue.
AppDelegate
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
let login = LoginViewController()
let navigationController = UINavigationController(rootViewController: login)
navigationController.setNavigationBarHidden(true, animated: true)
self.window = UIWindow(frame: UIScreen.main.bounds)
self.window?.rootViewController = navigationController
self.window?.makeKeyAndVisible()
return true
}
LoginViewController
let dashboard = DashboardViewController()
self.navigationController?.pushViewController(dashboard, animated: true)

Appdelegate sigabrt not appearing anything ! in main storyboard

I am making a newsfeed and when I try to add a post it appears this alert[SIGABRT]and not go to the next viewController to write the post here it's a picture , any idea?
and those are some ss from the breakpoint and exception.
class AppDelegate: UIResponder, UIApplicationDelegate {
var window: UIWindow?
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
FirebaseApp.configure()
let authListener = Auth.auth().addStateDidChangeListener { auth, user in
let storyboard = UIStoryboard(name: "Main", bundle: nil )
if user != nil {
UserService.observeUserProfile(user!.uid) { UserProfile in
UserService.currentUserProfile = UserProfile
}
//
let controller = storyboard.instantiateViewController(withIdentifier: "MainTabBarController") as! UITabBarController
self.window?.rootViewController = controller
self.window?.makeKeyAndVisible()
} else {
UserService.currentUserProfile = nil
let controller = storyboard.instantiateViewController(withIdentifier: "MenuViewController") as! ViewController
self.window?.rootViewController = controller
self.window?.makeKeyAndVisible()
}
}
return true
}
}

Changed from Navigation to TabBar, CoreData does not load

I changed my Navigation Controller to Tab Bar Controller, then I wanted to compile the app. But it does not load my managedContext anymore.
This is my launching code when I load the app:
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
guard let navController = window?.rootViewController as? UINavigationController,
let viewController = navController.topViewController as? ViewController else {
return true
}
viewController.managedContext = coreDataStack.managedContext
// Override point for customization after application launch.
return true
}
Therefore I tried to edit the code like this:
Edit:
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
let storyboard = UIStoryboard(name: "Main", bundle: nil)
let controller = storyboard.instantiateInitialViewController()
//let controller = storyboard.instantiateViewController(withIdentifier: "TabBarVC")
self.window?.rootViewController = controller
guard let tabController = window?.rootViewController as? UITabBarController,
let navController = tabController.viewControllers![0] as? UINavigationController,
let viewController = navController.topViewController as? ViewController else {
return true
}
// Version that works for UINavigationCOntroller
// guard let navController = window?.rootViewController as? UINavigationController,
// let viewController = navController.topViewController as? ViewController else {
// return true
// }
viewController.managedContext = coreDataStack.managedContext
// Override point for customization after application launch.
return true
}
I changed the code, but I still can not load managedContext, and it returns nil when I want to use it in ViewController, I also set the initial Viewcontroller to the tabBarController.
Set rootviewcontroller for your window first and then right your logic afterwards.
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
let storyboard = UIStoryboard(<StoryboardName>, bundle: nil)
let controller = storyboard.initializeInitialViewController()
window.rootViewController = controller
guard let navController = window?.rootViewController as? UITabBarController,
let viewController = navController.moreNavigationController as? ViewController else {
return true
}
viewController.managedContext = coreDataStack.managedContext
// Override point for customization after application launch.
return

Setting Intialview controller embedded in UINavigationController

I have few view controllers embedded in UINavigationController. The first view controller is login page. The second view controller is the home page. I want initialview controller as second view controller when the user is already logged in.
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool
{
// Override point for customization after application launch.
if let data = Locksmith.loadDataForUserAccount(userAccount: "someString")
{
if let userAccessToken = data["accessToken"]
{
if (userAccessToken as! String) != ""
{
let initialViewController = UIStoryboard(name: "Main", bundle: nil).instantiateViewController(withIdentifier: "HomePageViewController") as! HomePageViewController
loginResponse = LoginResponse()
loginResponse?.UserAccessToken = userAccessToken as? String
self.window = UIWindow(frame: UIScreen.main.bounds)
self.window?.rootViewController = initialViewController
self.window?.makeKeyAndVisible()
}
}
}
return true
}
The problem is the subsequent view controllers are not embedded in navigation controller. Since it is not embedded in navigation controller I am not able to goback from one view controller to the other.
Add this in App delegate
first check user already login or not, if login then execute this code
let mainStoryBoard = UIStoryboard(name: "Main", bundle: nil)
let redViewController = mainStoryBoard.instantiateViewControllerWithIdentifier("respectiveIdentifier") as! ViewController
let appDelegate = UIApplication.sharedApplication().delegate as! AppDelegate
appDelegate.window?.rootViewController = redViewController
Use this in App Delegate and add an extension of UIStoryboard.
func checkForAlreadyLogin() {
let dashBoardScreen = UIStoryboard.dashBoardScreen()
let loginController = UIStoryboard.loginController()
if UserDefaults.standard.bool(forKey: UserDefaultValues().LOGINSTATUS){
self.window!.rootViewController = dashBoardScreen
}else {
self.window!.rootViewController = loginController
}
}
public extension UIStoryboard {
class func mainStoryboard() -> UIStoryboard { return UIStoryboard(name: "Main", bundle: Bundle.main) }
class func dashBoardScreen() -> HomeViewController?{
return mainStoryboard().instantiateViewController(withIdentifier: "HomeViewController") as? HomeViewController
}
internal class func loginController() -> LoginViewController?{
return mainStoryboard().instantiateViewController(withIdentifier: "LoginViewController") as? LoginViewController
}
}
Just call this checkForAlreadyLogin() method in 'didFinishLaunchingWithOptions'.
Also remember to set StoryboardID in the storyboard for each viewController.
This code did the trick for me.
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
// Override point for customization after application launch.
if let data = Locksmith.loadDataForUserAccount(userAccount: "someString")
{
if let userAccessToken = data["accessToken"]
{
if (userAccessToken as! String) != ""
{
let initialViewController = UIStoryboard(name: "Main", bundle: Bundle.main).instantiateViewController(withIdentifier: "HomePageViewController") as! HomePageViewController
let navigationController = self.window?.rootViewController as! UINavigationController
navigationController.pushViewController(initialViewController, animated: true)
}
}
}
return true
}

how to create very first view for Swift 3

Hi I have made a ViewController.
But I don't know how to add very first view right after an app is tapped.
I added below codes in ViewController.swift
let storyboard = UIStoryboard(name: "Main", bundle: nil)
let controller = storyboard.instantiateViewController(withIdentifier: "FirstViewController")
self.present(controller, animated: true, completion: nil)
the FirstViewController appears after the app tapped and shows white background for about a second.
When you first launch your app, the LaunchScreen.storyboard is displayed, as a transition while your app is loading. You can customise this in Interface Builder.
check the option 'is initial view Controller' for that firstViewController from the storyboard.
You can set initial view controller programmatically or via storyboard
Method 1:programmatically--
Set storyboard-identifire for Viewcontroller in Main.storyboard file
like
After that set rootviewController in Appdelegate class.
import UIKit
#UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate {
var window: UIWindow?
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
window = UIWindow(frame: UIScreen.main.bounds)
let mainStoryBoard = UIStoryboard(name: "Main", bundle: nil)
let redViewController = mainStoryBoard.instantiateViewController(withIdentifier: "ViewController") as! ViewController
window?.rootViewController = redViewController
window?.makeKeyAndVisible()
return true
}
}
For setting up with navigation controller use UINavigationController -
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
window = UIWindow(frame: UIScreen.main.bounds)
let mainStoryBoard = UIStoryboard(name: "Main", bundle: nil)
let viewController = mainStoryBoard.instantiateViewController(withIdentifier: "viewController") as! viewController
let navigationController = UINavigationController(rootViewController: viewController)
window?.rootViewController = navigationController
window?.makeKeyAndVisible()
return true
}
Method 2:Via storyboard--
Go to respective storyboard & select Is Initial View Controller