-3

I am implementing an iOS app. when User Launch the application first time, I need to navigate to another page and from next time onwards I need to navigate Home Page. How to do this task in iOS swift?

gopal
  • 45
  • 1
  • 7

6 Answers6

1

Updated: if you get this error: "Value of type 'AppDelegate' has no member 'window' ".

Answer is : you have to write your codes inside of SceneDelegate.swift

func scene(_ scene: UIScene, willConnectTo session: UISceneSession, options connectionOptions: UIScene.ConnectionOptions) {

    let defaults = UserDefaults.standard


    guard  let a=UserDefaults.standard.object(forKey: "wantOverview")  else {



        let storyBoard = UIStoryboard.init(name: "Main", bundle: nil)
          let initialVc = storyBoard.instantiateViewController(withIdentifier: "firstTimeViewController") as! firstTimeViewController
          self.window?.rootViewController = initialVc
          self.window?.makeKeyAndVisible()


          defaults.set(false, forKey: "wantOverview")
        return
      }


      let storyBoard = UIStoryboard.init(name: "Main", bundle: nil)
            let initialVc = storyBoard.instantiateViewController(withIdentifier: "ViewController") as! ViewController
            self.window?.rootViewController = initialVc
            self.window?.makeKeyAndVisible()

    guard let _ = (scene as? UIWindowScene) else { return }
}
Erhan Demirci
  • 4,173
  • 4
  • 36
  • 44
0

This can be achieved using userdefaults. Just make a new controller like SplashViewController which will only redirect to desired ViewController.

import UIKit

class SplashViewController: UIViewController {

    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view.
        decideWhichScreenToShow()
    }

    // This logic will do
    func decideWhichScreenToShow(){
        if !UserDefaults.standard.bool(forKey: "your_key"){
            // This will execute first time, in that controller's viewDidLoad() make this true
        }else{

        }
    }
}

You can write same logic in Appdelegate.

Sharad Chauhan
  • 4,821
  • 2
  • 25
  • 50
  • You should remove the irrelevant method to be clearer : `didReceiveMemoryWarning()` `preferredStatusBarStyle` `prefersHomeIndicatorAutoHidden()` – Acrasia Apr 27 '18 at 08:09
0

Create func for checking that if your key in UserDefaults or not using this link & Then check by if...else

func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {

    window = UIWindow(frame: UIScreen.main.bounds)
    window?.makeKeyAndVisible()

    if isKeyPresentInUserDefaults(key: "isFirstTime") {
        let home = HomeVC()
        window?.rootViewController = home
    } else {
        let login = LoginVC()
        window?.rootViewController = login
        UserDefaults.standard.set(true, forKey: "isFirstTime")
    }

    return true
}

func isKeyPresentInUserDefaults(key: String) -> Bool {
    return UserDefaults.standard.object(forKey: key) != nil
}
Jay Patel
  • 2,642
  • 2
  • 18
  • 40
0

You may use change the didFinishLaunchingWithOptions method in AppDelegate to achieve this.

func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {

    self.window = UIWindow(frame: UIScreen.main.bounds)
    guard let firstTime = UserDefaults.standard.object(forKey: "IsFirstTime")  else {
        UserDefaults.standard.set(true, forKey: "IsFirstTime")
        let storyBoard = UIStoryboard.init(name: "FirstStoryBoard", bundle: nil)
        let initialVc = storyBoard.instantiateViewController(withIdentifier: "FirstControllerId") as! FirstTimeViewController
        self.window?.rootViewController = initialVc
        self.window?.makeKeyAndVisible()
        return true
    }
    let storyBoard = UIStoryboard.init(name: "Main", bundle: nil)
    let initialVc = storyBoard.instantiateViewController(withIdentifier: "NormalInitialController") as! ViewController
    self.window?.rootViewController = initialVc
    self.window?.makeKeyAndVisible()
    return true

}

You check if the value for key, "IsFirstTime", exists in UserDefaults. There won't be a value if the app is launching for the first time. In this case, you can launch your FirstTimeViewController and set a value to the key in UserDefaults.

If a value exists in UserDefaults, just initiate your normal ViewController.

picciano
  • 22,341
  • 9
  • 69
  • 82
SWAT
  • 1,107
  • 8
  • 19
0

Try this:

func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {


    if firstTime = UserDefaults.standard.object(forKey: "firstTime")
    {
        let storyboard = UIStoryboard(name: "SecondStoryBoard", bundle: nil)
        let MainView = storyboard.instantiateViewController(withIdentifier: "SecondTimeViewController") as! SecondTimeViewController

        let navController = UINavigationController.init(rootViewController: MainView)

        self.window?.rootViewController = navController

    }
    else
    {
        UserDefaults.standard.set(true, forKey: "firstTime")

        let storyboard = UIStoryboard(name: "FirstStoryBoard", bundle: nil)
        let MainView = storyboard.instantiateViewController(withIdentifier: "FirstTimeViewController") as! FirstTimeViewController

        let navController = UINavigationController.init(rootViewController: MainView)

        self.window?.rootViewController = navController

    }

}
Nick
  • 875
  • 6
  • 20
0

The following solution to this common problem assumes that

  1. At the outset — in this example, that's before the "username" key has been set in the user defaults — we want to launch to the storyboard's Initial View Controller (where the user is supposed to set the "username" key).

  2. Thereafter, we always want to launch to a different view controller in the storyboard, whose identifier in this example is "root".

It's very short and simple!

class AppDelegate: UIResponder, UIApplicationDelegate {
    var window: UIWindow?
    func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
        if let rvc = self.window?.rootViewController {
            if UserDefaults.standard.object(forKey:"username") as? String != nil {
                self.window!.rootViewController = rvc.storyboard!.instantiateViewControllerWithIdentifier("root")
            }
        }
        return true
    }
}
matt
  • 515,959
  • 87
  • 875
  • 1,141