Setting Intialview controller embedded in UINavigationController - swift

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
}

Related

Different view controller display at start app

I try to explain situation:
I have two view controllers: viewHome and viewStartTest.
When student start app first time, don't have any data about his test in table.
In this situation should be display viewStartTest controller after launch screen.
But when he start app again and condition "test is finished" is true, viewHome controller should be display at start.
I try to put this code in AppDelegate.swift and simulate finished test but still not working, thanks for help:
// 0 - false, 1 - True
var conditionTest = 1
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
if conditionTest == 1 {
self.window = UIWindow(frame: UIScreen.main.bounds)
let mainStoryboard: UIStoryboard = UIStoryboard(name: "Main", bundle: nil)
let viewStartTest: UIViewController = mainStoryboard.instantiateViewController(withIdentifier: "viewStartTest")
self.window?.rootViewController = viewStartTest
self.window?.makeKeyAndVisible()
} else {
self.window = UIWindow(frame: UIScreen.main.bounds)
let mainStoryboard: UIStoryboard = UIStoryboard(name: "Main", bundle: nil)
let ViewHome: UIViewController = mainStoryboard.instantiateViewController(withIdentifier: "viewHome")
self.window?.rootViewController = ViewHome
self.window?.makeKeyAndVisible()
}
}
Correct Scene delegate code after discussion below:
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).
if conditionTest == 1 {
let mainStoryboard: UIStoryboard = UIStoryboard(name: "Main", bundle: nil)
let viewStartTest: UIViewController = mainStoryboard.instantiateViewController(withIdentifier: "viewStartTest")
self.window?.rootViewController = viewStartTest
self.window?.makeKeyAndVisible()
} else {
let mainStoryboard: UIStoryboard = UIStoryboard(name: "Main", bundle: nil)
let ViewHome: UIViewController = mainStoryboard.instantiateViewController(withIdentifier: "viewHome")
self.window?.rootViewController = ViewHome
self.window?.makeKeyAndVisible()
}
}
guard let _ = (scene as? UIWindowScene) else { return }
}
You need to store the value of conditionTest somewhere. I would suggest using UserDefaults . This is an example of how you could implement it:
NavigationController:
class MainNavigationControllerViewController: UINavigationController {
override func viewDidLoad() {
super.viewDidLoad()
if isLoggedIn() {
let homeController = UIStoryboard(name: "Main", bundle: nil).instantiateViewController(withIdentifier: "HomeVC")
viewControllers = [homeController]
}
}
fileprivate func isLoggedIn() -> Bool {
return UserDefaults.standard.isLoggedIn()
}
}
Extension for UserDefaults:
extension UserDefaults {
func setIsLoggedIn(value: Bool) {
set(value, forKey: "isLoggedIn")
synchronize()
}
func isLoggedIn() -> Bool {
return bool(forKey: "isLoggedIn") }
}
How to use:
with the above code you can simply login/logout the user. Make sure to call .synchronize()
Login:
UserDefaults.standard.setIsLoggedIn(value: true)
UserDefaults.standard.synchronize()
Logout:
UserDefaults.standard.setIsLoggedIn(value: false)
UserDefaults.standard.synchronize()

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
}
}

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

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
}

PageViewController (app presentation) for the first launch then tabBarViewController, Swift

I have a PageViewController with 3 views to present the app, on the last page I have a button linked with my TabBarViewController.
I would like to have only for the first launch, my 3 views of presentation and then for the next launched, start directly with my TabBarViewController.
How can I do it?
Thanks!!!
EDIT
tabBarController :
required init(coder aDecoder: NSCoder)
{
super.init(coder: aDecoder)!
}
Button between page views and tabBar :
#IBAction func firstTime() {
NSUserDefaults.standardUserDefaults().setBool(true, forKey: "hasAppBeenLaunchedBefore")
print(true)
}
AppDelegate :
func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
// Override point for customization after application launch.
didFinishLaunchingOnce()
return true
}
func didFinishLaunchingOnce() -> Bool
{
let defaults = NSUserDefaults.standardUserDefaults()
if let hasBeenLauncherBefore = defaults.stringForKey("hasAppBeenLaunchedBefore")
{
self.window = UIWindow(frame: UIScreen.mainScreen().bounds)
let mainStoryboard: UIStoryboard = UIStoryboard(name: "Main", bundle: nil)
var exampleViewController: TabBarViewController = mainStoryboard.instantiateViewControllerWithIdentifier("TabBarViewController") as! TabBarViewController
self.window?.rootViewController = exampleViewController
self.window?.makeKeyAndVisible()
//print(" N-th time app launched ")
return true
}
else
{
self.window = UIWindow(frame: UIScreen.mainScreen().bounds)
let mainStoryboard: UIStoryboard = UIStoryboard(name: "Main", bundle: nil)
var exampleViewController: OnboardingPager = mainStoryboard.instantiateViewControllerWithIdentifier("OnboardingPager") as! OnboardingPager
self.window?.rootViewController = exampleViewController
self.window?.makeKeyAndVisible()
//print(" First time app launched ")
defaults.setBool(true, forKey: "hasAppBeenLaunchedBefore")
return false
}
}
Enjoy

MMDrawer Swift 2 Redirect to MMDrawer How to set on Click

I want to redirect login view controller to mmdrawer controller in swift
if I am making only mmdrawer it works well because the code is under Appdelegate is working perfect .
var window: UIWindow?
var centerContainer: MMDrawerController?
func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
// Override point for customization after application launch.
_ = self.window!.rootViewController
let mainStoryboard: UIStoryboard = UIStoryboard(name: "Main", bundle: nil)
// let centerViewController = mainStoryboard.instantiateViewControllerWithIdentifier("LoginPageViewController") as! LoginView
//var rootViewController = centerViewController
let centerViewController = mainStoryboard.instantiateViewControllerWithIdentifier("CenterController") as! ViewController
let leftViewController = mainStoryboard.instantiateViewControllerWithIdentifier("LeftSideViewController") as! LeftSideDrawer
let leftSideNav = UINavigationController(rootViewController: leftViewController)
let centerNav = UINavigationController(rootViewController: centerViewController)
centerContainer = MMDrawerController(centerViewController: centerNav, leftDrawerViewController: leftSideNav)
centerContainer!.openDrawerGestureModeMask = MMOpenDrawerGestureMode.PanningCenterView;
centerContainer!.closeDrawerGestureModeMask = MMCloseDrawerGestureMode.PanningCenterView;
window!.rootViewController = centerContainer
window!.makeKeyAndVisible()
return true
but my question is how to add login view controller so can redirect it to mmdrawer on sucess .
Thanks