view tab bar controller from storyboard swift 5 - ios

I am working on swift 5 and xcode 11.5
I am trying to move user from main storyboard to tab bar controller if he is logged in,
in my appDelegate I tried this
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
if let api_token = helper.getApiToken() {
print("api_token: \(api_token)")
let tab = UIStoryboard(name: "Main", bundle: nil).instantiateViewController(identifier: "maintab")
window?.rootViewController = tab
}
return true
}
here I want to check if there is a user api_token, if yes move to the tab bar
if no show the main storyboard
but it always goes to main.storyboard even if there is a token!

Try this code
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
if let api_token = helper.getApiToken() {
print("api_token: \(api_token)")
let storyboard = UIStoryboard(name: "Main", bundle: nil)
let tab = storyboard.instantiateViewController(withIdentifier: "maintab")
window?.rootViewController = tab
}
return true
}

Related

setting initial viewcontroller in swift [duplicate]

This question already has an answer here:
Xcode 11 & iOS13, using UIKIT can't change background colour of UIViewController
(1 answer)
Closed 3 years ago.
im trying to set the initial view controller if a user has a token in his keychain.
when I try to set it in the appDelegate like this
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
let accessToken: String? = KeychainWrapper.standard.string(forKey: "accessToken")
if accessToken != nil
{
// Take user to a home page
let mainStoryboard:UIStoryboard = UIStoryboard(name: "Main", bundle: nil)
let homePage = mainStoryboard.instantiateViewController(withIdentifier: "home") as! ViewLoader
self.window.rootViewController = homePage
}
return true
}
but I get som errors saying "Value of type 'AppDelegate' has no member 'window'"
it tried to clean the project but without luck
Try this:
window = UIWindow(frame: UIScreen.main.bounds)
let mainStoryboard:UIStoryboard = UIStoryboard(name: "Main", bundle: nil)
let homePage = mainStoryboard.instantiateViewController(withIdentifier: "home") as! ViewLoader
window?.rootViewController = homePage
window?.makeKeyAndVisible()
Init window first and add the function makeKeyAndVisible.
The reason of your error is you do not declare a variable window.
Adding following code in your AppDelegate class is a solution for error:
class AppDelegate: UIResponder, UIApplicationDelegate {
var window: UIWindow?
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
let mainStoryboard:UIStoryboard = UIStoryboard(name: "Main", bundle: nil)
let homePage = mainStoryboard.instantiateViewController(withIdentifier: "home")
self.window?.rootViewController = homePage
self.window?.makeKeyAndVisible()
return true
}
// ..... more code ...
}

navigationController.pushViewController doesn't seem to work in Xcode 11?

I have created a basic single view application. Using Xcode 11 now. I always build apps programatically because the tutorials I have started with never use the interface builder. For some reason not able to get the pushViewController to work. Seems to work just fine in my other projects built using Xcode 10.
In App Delegate
class AppDelegate: UIResponder, UIApplicationDelegate {
var window: UIWindow?
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
window = UIWindow()
window?.makeKeyAndVisible()
let nc = UINavigationController(rootViewController: ViewController())
window?.rootViewController = nc
return true
}
In my viewController
#objc func handleChat(){
print("Chat pressed")
navigationController?.pushViewController(InboxViewController(), animated: true)
}
AppDelegate.swift
var window: UIWindow?
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
// Override point for customization after application launch.
let VC = UIStoryboard(name: "Main", bundle: nil).instantiateViewController(withIdentifier: "ViewController") as? ViewController
var Nav: UINavigationController? = nil
if let VC = VC {
Nav = UINavigationController(rootViewController: VC)
}
window?.rootViewController = Nav
return true
}
Remove SceneDelegate.swift
Goto Info.plist
Remove Application Scene Manifest
It's work for me.

how to know is a view is the firts view loaded or not in ios

is there a way to know if the view is first view in app after it get launched. to be more clear about it if in storybard i have a initial view controller set and in appdelegate
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
...some condition
let storyboard = UIStoryboard(name: "Main", bundle: nil)
let initialViewController = storyboard.instantiateViewController(withIdentifier: "TabBarVC")
self.window?.rootViewController = initialViewController
self.window?.makeKeyAndVisible()
}
what i want to know in the selected index viewcontroller if is comming from appdelegate or is come frome another view in the view hierarchy.

iOS state restoration issue with DrawerController

I have an app written in Swift 3.1, using Xcode 8.3.3.
I am currently trying to implement state preservation/restoration.
To do this I have implemented shouldSaveApplicationState and willFinishLaunchingWithOptions methods in AppDelegate.swift and set to return true:
// AppDelegate.swift
// STATE RESTORATION CALLBACKS
func application(_ application: UIApplication, shouldSaveApplicationState coder: NSCoder) -> Bool {
debug_print(this: "shouldSaveApplicationState")
return true
}
func application(_ application: UIApplication, shouldRestoreApplicationState coder: NSCoder) -> Bool {
debug_print(this: "shouldRestoreApplicationState")
restoringState = true
return true
}
func application(application: UIApplication, willFinishLaunchingWithOptions launchOptions: [NSObject : AnyObject]?) -> Bool {
debug_print(this: "willFinishLaunchingWithOptions")
return true
}
I’ve also provided restoration IDs for all involved viewcontrollers and navigationcontrollers.
I'm using a 3rd party library to handle side drawer navigation container (https://github.com/sascha/DrawerController). The initial viewcontroller is set programmatically inside the didFinishLaunchingWithOptions method, see below.
// AppDelegate.swift
var centerContainer: DrawerController?
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
let mainStoryboard: UIStoryboard = UIStoryboard(name: "Main", bundle: nil)
let centerViewController = mainStoryboard.instantiateViewController(withIdentifier: "RootViewControllerNav") as! UINavigationController
let leftViewController = mainStoryboard.instantiateViewController(withIdentifier: "SideDrawerViewController") as! UITableViewController
centerContainer = DrawerController(centerViewController: centerViewController, leftDrawerViewController: leftViewController)
centerContainer?.restorationIdentifier = "DrawerControllerView"
window = UIWindow(frame: UIScreen.main.bounds)
window?.restorationIdentifier = "MainWindow"
window?.rootViewController = centerContainer
window?.makeKeyAndVisible()
return true
}
When app opens and attempts to restore state, it displays the correct viewcontroller (last controller before app closed) temporarily, then once app becomes active it reverts back to the initial viewcontroller.
For example, the following happens:
Open app Navigate to the “settings” view via the side menu
Navigate to the home screen
Stop running xcode and start it again
App will open showing settings view, then revert back to home view
Can anyone tell me what is causing this, or where I am going wrong? Let me know if you need any more code examples.
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
// Override point for customization after application launch.
let mainStoryboard: UIStoryboard = UIStoryboard(name: "Main", bundle: nil)
let leftSideDrawerViewController = mainStoryboard.instantiateViewController(withIdentifier: "SideDrawerViewController") as! UITableViewController
let centerViewController = mainStoryboard.instantiateViewController(withIdentifier: "RootViewControllerNav") as! UINavigationController
let navigationController = UINavigationController(rootViewController: centerViewController)
navigationController.restorationIdentifier = "navigationControllerKey"
let leftSideNavController = UINavigationController(rootViewController: leftSideDrawerViewController)
leftSideNavController.restorationIdentifier = "LeftNavigationController"
self.drawerController = DrawerController(centerViewController: navigationController, leftDrawerViewController: leftSideNavController, rightDrawerViewController: nil)
self.drawerController.openDrawerGestureModeMask = .all
self.drawerController.closeDrawerGestureModeMask = .all
self.window = UIWindow(frame: UIScreen.main.bounds)
self.window?.rootViewController = self.drawerController
return true
}

How to set the rootViewController with Swift, iOS 7

I want to set the rootViewController in the app delegate ..
func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: NSDictionary?) -> Bool {
var rootView: MyRootViewController = MyRootViewController()
//Code to set this viewController as the root view??
return true
}
If you are using a storyboard and want to set your rootViewController programmatically, first make sure the ViewController has a Storyboard ID in the Identity Inspector.
Then in the AppDelegate do the following:
func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
// get your storyboard
let storyboard = UIStoryboard(name: "Main", bundle: nil)
// instantiate your desired ViewController
let rootController = storyboard.instantiateViewControllerWithIdentifier("MyViewController") as! UIViewController
// Because self.window is an optional you should check it's value first and assign your rootViewController
if let window = self.window {
window.rootViewController = rootController
}
return true
}
You can do something like this.
func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: NSDictionary?) -> Bool {
var rootView: MyRootViewController = MyRootViewController()
if let window = self.window{
window.rootViewController = rootView
}
return true
}
Swift 2.0:
var window: UIWindow?
var storyboard:UIStoryboard?
func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
window = UIWindow(frame: UIScreen.mainScreen().bounds)
window?.makeKeyAndVisible()
storyboard = UIStoryboard(name: "Main", bundle: nil)
let rootController = storyboard!.instantiateViewControllerWithIdentifier("secondVCID")
if let window = self.window {
window.rootViewController = rootController
}
In order to get it to show there are some things you need to do if you are not using a storyboard. Inside the AppDelegate inside the function application.
func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
// Override point for customization after application launch.
let frame = UIScreen.mainScreen().bounds
window = UIWindow(frame: frame)
let itemsViewControler: UITableViewController = BNRItemsViewController()
if let window = self.window{
window.rootViewController = itemsViewControler
window.makeKeyAndVisible()
}
return true
}
if let tabBarController = self.window!.rootViewController as? UITabBarController
{
tabBarController.selectedIndex = 0
}

Resources