2

Trying to programmatically set the initially view controller in AppDelegate's didFinishLaunchingWithOptions(..) function - followed the answers here and here, but running into the problem where when debugging instead of showing the "Home" VC, a black screen loads instead. I have tried to look for answers to the "black screen" issue that others have experienced, but no luck.

Here is my AppDelegate's didFinishLaunchingWithOptions :

func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
    // Override point for customization after application launch.
    FirebaseApp.configure()
    ApplicationDelegate.shared.application(application, didFinishLaunchingWithOptions: launchOptions)


    let frame = UIScreen.main.bounds
    let window = UIWindow(frame: frame)

    let storyBoard : UIStoryboard = UIStoryboard(name: "Home", bundle: nil)
    let homeVC = storyBoard.instantiateViewController(identifier: "Home")

    window.rootViewController = homeVC
    window.makeKeyAndVisible()

    return true
}

My "Home" VC in the "Home" Storyboard has "Is Initial View Controller" disabled/unchecked.

My Project's "Main Interface" is left empty, under the "Deployment Info" section of "General" settings.

I can see that my "Home" VC's viewDidLoad() override function loads all the way through - but, the Storyboard itself does not load (is white), before the black screen is produced.

Help? Not sure what else may be relevant.

// Home.swift:

import Foundation
import UIKit
import Firebase

class Home: UIViewController {

    override func viewDidLoad() {
        super.viewDidLoad()
        print("Something!")
        self.view.backgroundColor = .green
    }
}

Home VC Storyboard

Console output:

<Warning>: Please set a value for FacebookAutoLogAppEventsEnabled. Set the flag to TRUE if you want to collect app install, app launch and in-app purchase events automatically. To request user consent before collecting data, set the flag value to FALSE, then change to TRUE once user consent is received. Learn more: https://developers.facebook.com/docs/app-events/getting-started-app-events-ios#disable-auto-events.
<Warning>: You haven't set a value for FacebookAdvertiserIDCollectionEnabled. Set the flag to TRUE if you want to collect Advertiser ID for better advertising and analytics results. To request user consent before collecting data, set the flag value to FALSE, then change to TRUE once user consent is received. Learn more: https://developers.facebook.com/docs/app-events/getting-started-app-events-ios#disable-auto-events.
- <AppMeasurement>[I-ACS036002] Analytics screen reporting is enabled. Call +[FIRAnalytics setScreenName:setScreenClass:] to set the screen name or override the default screen class name. To disable screen reporting, set the flag FirebaseScreenReportingEnabled to NO (boolean) in the Info.plist
6.10.0 - [Firebase/Analytics][I-ACS023007] Analytics v.60103000 started
6.10.0 - [Firebase/Analytics][I-ACS023008] To enable debug logging set the following application argument: -FIRAnalyticsDebugEnabled (see ..)
FBSDKLog: Unable to find a valid UIWindow
Something!
[WindowScene] Failed to instantiate the default view controller for UIMainStoryboardFile 'Main' - perhaps the designated entry point is not set?
Unbalanced calls to begin/end appearance transitions for <ProjectName.Home: 0x7ffa2640ff90>.

Editing to clarify:

The point in attempting to try to programmatically set the initial VC is so that any VC can be set. This is useful in scenarios such as when the app initially loads, checking if the user is logged in or not, and depending on the token, showing the respective screen.

Trying to avoid having an intermediate VC that does the forwarding, because with iOS 13, the order of windows is layered.

Thank you everyone!

Dash
  • 182
  • 3
  • 14
  • you need to have a reference for your `UIWindow`. – koropok Oct 15 '19 at 01:19
  • 1
    If you're using xcode 11 this is ap ossible duplicate of [Xcode 11 & iOS13, using UIKIT can't change background colour of UIViewController](https://stackoverflow.com/questions/58251425/xcode-11-ios13-using-uikit-cant-change-background-colour-of-uiviewcontroller) If you're not, then you're improperly setting the window. `let window = UIWindow(frame: frame)` should be `window = UIWindow(frame: frame)` – valosip Oct 15 '19 at 02:03
  • Thank you, @valosip ! Didn't realize that the rootViewController is to be set in the scene delegate, not the app delegate. – Dash Oct 15 '19 at 02:21
  • You can do following way to set your view controller in ios 11 in scene delegate func setupMainVC() { let storyboard = UIStoryboard.init(name: "Login", bundle: nil) guard let rootVC = storyboard.instantiateViewController(identifier: "LoginVC") as? LoginVC else { print("Lanaguge VC not found") return } let rootNC = UINavigationController(rootViewController: rootVC) rootNC.navigationBar.isHidden = true self.window?.rootViewController = rootNC self.window?.makeKeyAndVisible() } – Anita Oct 15 '19 at 03:58

1 Answers1

1

In your AppDelegate It should be

window = UIWindow(frame: frame)

instead of

let window = UIWindow(frame: frame)
Tung Vu Duc
  • 1,204
  • 1
  • 11
  • 20