0

lets say I have MainVC1 and MainVC2 and depending on user type I decide to Load one of them on app start, there isn't much to do to find out which to choose , but I guess its not right to put this on AppDelegate , so I created a VC name PreMain witch decides this , but the problem is that AppStartUp is Ugly cause a VC comes and disappear fast (thePreMain) and then of of the Mains will start , is there any better way to do this ? or is it possible to have an ViewController without a actual View ?

Mahan
  • 147
  • 1
  • 3
  • 14
  • Your idea is right, just make your preMain VC looks like a loading screen, similar/exact as LaunchScreen. This is common practice, giving you some time to handle business logic/ ui items loadings. – dvp.petrov Sep 28 '17 at 13:52

3 Answers3

1

Beset way is changing the rootViewController in AppDelegate

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


    var initialViewController: UIViewController!
    let storyboard = UIStoryboard(name: "Main", bundle: nil)

    if somecondition {
      initialViewController = storyboard.instantiateViewController(withIdentifier: "FirstViewController")//replace your FirstViewController identifier
    } else {
      initialViewController = storyboard.instantiateViewController(withIdentifier: "SecondViewController")//replace your SecondViewController identifier
    }

    window?.rootViewController = initialViewController
    window?.makeKeyAndVisible()

    return true
  }
RajeshKumar R
  • 15,445
  • 2
  • 38
  • 70
1

but I guess its not right to put this on AppDelegate

It is nothing wrong to choose AppDelegate to do that logic, after all at the didFinishLaunching, you are free to do whatever.

And like you said, the PreMain will briefly show up.

Here's the link that can help you Programmatically set the initial view controller using Storyboards

To avoid any "VC comes and disappear fast"

Matthew Yeung
  • 58
  • 1
  • 5
1

but I guess its not right to put this on AppDelegate

actually that's not right! if you tried to make a workaround as instantiating a pre View Controller -as you mentioned-, that would be the "not right" case.

As mentioned in the application(_:didFinishLaunchingWithOptions:) documentation:

Tells the delegate that the launch process is almost done and the app is almost ready to run.

which seems to be a good starting point to determine the desired initial View controller. You might want to check: set initial viewcontroller in appdelegate - swift Q&A for achieving it, all you have to do is to implement the logic of choosing the desired view controller.

Ahmad F
  • 30,560
  • 17
  • 97
  • 143
  • yea you are right the delegate should do the thing I was wrong about it thanks – Mahan Sep 28 '17 at 12:58
  • Tried this but it only shows black screen when setting storyboard as rootview – Mahan Sep 28 '17 at 16:00
  • Have you tried the answers in the question I mentioned? not only the accepted one. – Ahmad F Sep 28 '17 at 16:43
  • yea tried them all , i wasnt using storyboard as launcher before so it is deleted from info.plist , can this be the problem ? dont think so im setting the rootview myself – Mahan Sep 28 '17 at 16:49
  • Fixed it , I had forgotten to make one of them the initial VC – Mahan Sep 29 '17 at 03:47