UIWindow results in a black screen when changing the rootViewController - ios

I'll start with the main problem: My issue is not easily reproducible.
So... In my app, depending on whether it's the first time the user opens it, I am changing the rootViewController.
I do so like this:
#UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate {
var window: UIWindow?
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
if /* code to figure out if it's the first launch */ {
let storyboard = UIStoryboard(name: "Onboarding", bundle: nil)
let onboardingViewController = storyboard.instantiateInitialViewController()
window?.rootViewController = onboardingViewController
}
return true
}
}
And this basically works great! As I am currently building that Onboarding ViewController, I removed the if and therefore it always get's displayed. This also worked fine, but all of a sudden the app would not start any more. The screen stays black. I then run the exact same code on an other device and it's working there.
Btw. the code above is all there is in my AppDelegate and the issues persists if the OnboardingVC has everything commented out.
When looking at the UI-Debugger for my app it also looks strange:
Normally there would be an arrow next to the UIWindow showing that it's root is indeed my VC, but in my case that's not the case.
I wouldn't mind and just delete the app from my device and reinstall it afterwards, but I've had one user-complaint for an other app of mine which users the same logic, who told me about the exact problem. Now I basically reproduced the issue but have no idea on where the problem should be.
My app is using storyboards btw.
Maybe somebody had that problem before and has any idea on what might be the problem.
EDIT:
Just had the problem again today... Here are some more observations:
When I initiate my ViewController and output it's view, it's not correct. It's not the view that is really set in the storyboard. It's a random other view.
Is it possible, that the storyboard is corrupt? And if so... what can I do about it?
After I added a random subview to my Main-View-Controllers view and run again, it worked again... So obviously the storyboard somehow got corrupt?
Ok... while development... ok... but for an app that's out there and all of a sudden the storyboard get's corrupt... that is really bad!!!!
Did anybody have that problem before? And how to solve it?

try this
in your case window is nil so
#UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate {
var window: UIWindow?
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
window = UIWindow(frame: CGRect(x: 0, y: 0, width: UIScreen.main.bounds.size.width, height: UIScreen.main.bounds.size.height))
if /* code to figure out if it's the first launch */ {
let storyboard = UIStoryboard(name: "Onboarding", bundle: nil)
let onboardingViewController = storyboard.instantiateInitialViewController()
window?.rootViewController = onboardingViewController
}
return true
}
}

I've finally figured out the problem. For whatever reason I obviously double-linked the view outlet of my Initial-View-Controller to a subview. So it had two outlets to the view property (how is that even possible?)
And it randomly picked on or the other on startup....
Took a very sharp eye of a friend to see the problem :)

Related

How to set a UITabBarController as rootViewController from Storyboard

Short question:
How can I launch and make a UITabBarController be the rootViewController of my app after starting with a Storyboard?
Long question:
I'm not a swift expert, but I managed to create a complete app using XIBs from the beginning. Now I need my app to start with a Storyboard as a new requirement to post updates to the appstore from 01/07/2020, but I never used it to build my views. It was easy to modify my app to have my Storyboard as an entry point, but the problem is that my initial view today is a TabController, and I don't know how to navigate from my initial Storyboard to my TabController.
My AppDelegate today works something like this:
var window: UIWindow?
func application(_application: UIApplication, didFinishLauchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
self.window = UIWindow(frame: UIScreen.main.bounds)
// initiate UINavigationControllers and UITabBarController here...
tabController.viewController = [nav1, nav2, nav3, nav4]
tabController.modalPresentationStyle = .fullScreen
self.window!.rootViewController = tabController
self.window!.makeKeyAndVisible()
}
All my attempts ended with a white screen after showing my Storyboard without showing my TabBar.
One of these attempts was this:
override func loadView() {
super.loadView()
// initiate tabController the same way I did in the AppDelegate
UIApplication.shared.keyWindow?.rootViewController = tabController
}
Check the value "Is initial View Controller" for it.

Failed to instantiate view controller. - Swift

So, I'm trying to instantiate my view controller programmatically using storyboard references.
I've put this code in the AppDelegate:
var window: UIWindow?
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
window = UIWindow(frame: UIScreen.main.bounds)
let mainStoryboard: UIStoryboard = UIStoryboard(name: "Main", bundle: nil)
let initialController: WelcomePageViewController = mainStoryboard.instantiateViewController(withIdentifier: "WelcomePageViewController") as! UITabBarController
window?.rootViewController = initialController
window?.makeKeyAndVisible()
return true
}
And set this inside my view controller's storyboard:
However when I run the application, only a black screen is shown and this message:
"Failed to instantiate the default view controller for UIMainStoryboardFile 'Main' - perhaps the designated entry point is not set?"
I've used this exact code in other apps and it works just fine.
I've tried to clean build folder, to run it on an actual device and to create and instantiate a different storyboard file but nothing worked.
Open your Main.storyboard file and find WelcomePageViewController. When it’s selected, go to the attributes inspector and check the box marked Is Initial View Controller.
You should see a right-facing arrow appear to the left of WelcomePageViewController, showing that it’s your storyboard’s entry point.
Now you're all set to go!!!
Ok So i think here is something you can do if it suits:
set entry point to the navigation controller
class that navigation controller with a file like rootNavigationVC.swift which is subclass of UINavigationController
now in didLoad() you can set it's viewController or push one based on your condition
trust me its better than coding in AppDelegate.swift which never worked for me.

Programmatically set the initial view controller using Storyboards in Xcode 11.2 [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.
I am trying to programmatically set the initial View controller but i keep getting this Error. Any solutions?
2019-11-07 11:47:43.975990+0000 RestaurantApp[16319:147412] [WindowScene] Failed to instantiate the default view controller for UIMainStoryboardFile 'Main' - perhaps the designated entry point is not set?
Here is the code that i have Written.
import UIKit
#UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate {
let window = UIWindow()
let locationService = LocationService()
let storyboard = UIStoryboard(name: "Main", bundle: nil) //refernce to our storyboard
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
// Override point for customization after application launch.
//setiing the root view control on our window
switch locationService.status {
case .notDetermined, .denied, .restricted:
let LocationViewController =
storyboard.instantiateViewController (withIdentifier: "LocationViewController") as? LocationViewController
LocationViewController?.locationService = locationService
window.rootViewController = LocationViewController
default:
assertionFailure()
}
window.makeKeyAndVisible()
return true
}
}
Here is an Image of my storyboard
iOS 13 has moved the windows setup from AppDelegate to SceneDelegate to support the use of (possibly multiple) scenes rather than a single window. You now have to do the setup like this:
class SceneDelegate: UIResponder, UIWindowSceneDelegate {
var window: UIWindow?
let storyboard = UIStoryboard(name: "Main", bundle: nil)
func scene(_ scene: UIScene, willConnectTo session: UISceneSession, options connectionOptions: UIScene.ConnectionOptions) {
guard let windowScene = scene as? UIWindowScene else { return }
let vc = storyboard.instantiateViewController (withIdentifier: "Primary") as! ViewController
window = UIWindow(windowScene: windowScene)
window?.rootViewController = vc
window?.makeKeyAndVisible()
}
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
// Override point for customization after application launch.
let storyboard = UIStoryboard.init(name: "Main", bundle: nil)
let homeView = storyboard.instantiateViewController(withIdentifier: "ViewController") as! ViewController
self.window?.rootViewController = homeView
return true
}
this works for me
This error happens due to a simple mistake in your storyboard, and it’s easy to fix. When your app starts, iOS needs to know precisely which view controller needs to be shown first – known as your default view controller.
If you accidentally deleted that view controller, or otherwise made it not the default, then you’ll see the error “Failed to instantiate the default view controller for UIMainStoryboardFile 'Main' - perhaps the designated entry point is not set?” when your app launches, along with a plain black screen.
To fix the problem, open your Main.storyboard file and find whichever view controller you want to be shown when your app first runs. When it’s selected, go to the attributes inspector and check the box marked “Is Initial View Controller”. You should see a right-facing arrow appear to the left of that view controller, showing that it’s your storyboard’s entry point.
Go back to storyboard and checkmark this to make the viewController you want your app to start off to,

iOS App is creating two UIWindow

I was refactoring an existing project to move away from using .storyboards and removed the initial main interface in .plist, but for some reason the app is creating two instances of UIWindow.
I have no idea on why this is happening, and the result of this is when I do create my actual UIWindow and use makeKeyAndVisible() for a second I get a black screen until the actual rootViewController becomes visible, this happens because in that split of a seconds it shows the first UIWindow which color is nil. If someone has any idea on why this is happening I would appreciate a bunch ;)
EDIT 1:
AppDelegate.swift
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
window = UIWindow(frame: UIScreen.main.bounds)
appCoordinator.start()
return true
}
AppCoordinator.swift
func start() {
let mainVC = UIStoryboard(storyboard: .main).instantiateInitialViewController()
window.backgroundColor = .white
window.rootViewController = mainVC
window.makeKeyAndVisible()
}
Check that "Main Interface" is empty:

How can I change my initial View Controller when the user accepts the EULA

I hope this is a simple question. I have not found anything in SO that seems to fit what I'm trying to do. I am working on my first ever project so much of this is still quite new to me. The code I have pasted is from a test project I'm using before I add code to my real one.
My initial VC is the EULA. When the user accepts, I want to change the initial VC to the log in view. In this test project, I have two views with an IBAction for a Bar Button Item that will segue the project from VC to VC2. I am trying to set up code to change the initial VC in the AppDelegate with
func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
// Override point for customization after application launch.
let storyboard = UIStoryboard(name: "View Controller", bundle: NSBundle.mainBundle())
let licenseAccepted = ?
var vc: UIViewController?
if !licenseAccepted {
vc = storyboard.instantiateViewControllerWithIdentifier("View Controller2")
} else {
vc = storyboard.instantiateInitialViewController()
}
window = UIWindow(frame: UIScreen.mainScreen().bounds)
window?.rootViewController = vc
window?.makeKeyAndVisible()
return true
}
but this is not working. First, let licenseAccepted = ? is not correct. I found that in another set of code and I thought it might work, but no. Also, how do I set a var for if the user accepts the license in the AppDelegate? Should that be done in the initial VC, the second VC that will become the initial VC or somewhere else?
I am using Xcode 7.1 and Swift2 if that helps.
I may be way off base or I may be a keystroke or two away. I am just not getting my head around this. Your assistance will be appreciated.

Resources