Silver background showing between LaunchImage and TabBarViewController - swift

My tvOS app does not have or need a proper login screen but I do have a check that is called from AppDelegate at first launch. The issue I am having is while the checkUser is happening with my backend the entire app turns to a silver color. Is there anything I can do about this? I have checked all viewcontroller backgrounds and I dont believe they are causing this. If I cannot fix this I'm wondering I wouldn't mind it being black instead but have no idea what causes this.
Flow: LaunchImage > (silver bg I want to remove during AppDelagate) > TabBarViewController/FirstViewController
In my AppDelegate...
func application(_ application: UIApplication,
didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
checkUser();
}
func checkUser() {
//If user is valid...
self.window = UIWindow(frame: UIScreen.main.bounds)
let storyboard = UIStoryboard(name: "Main", bundle: nil)
let initialViewController = storyboard.instantiateViewController(withIdentifier: "TabBarViewController")
self.window?.rootViewController = initialViewController
self.window?.makeKeyAndVisible()
}

Set your rootViewController to something that you want to appear at the start (often apps will match their launch screen) before you call checkUser(). Then you can set the rootViewController to your TabBarViewController in the checkUser() when ready.
There's no reason you can't change the rootViewController many times during your app's life time.

Related

show the second ViewController when app launch the second time

so when i launch my app the first time there is a welcome ViewController. How can i set a function, that shows the second ViewController when the user launches the app the second time.
Add a Boolean in the user default in your application to check whether the application is launch first time or not. Based on this Boolean value load another ViewController in your app delegate class.
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
let isFirst = UserDefaults.standard.bool(forKey: “isLaunched”) // edited this after rmaddy's comment
var viewControllerWithIdentifier = "SecondViewController"
if !isFirst {
UserDefaults.standard.set(true, forKey: “isLaunched”)
viewControllerWithIdentifier = "FirstViewController"
}
let mainStoryboard : UIStoryboard = UIStoryboard(name: "Main", bundle: nil)
let initialViewController : UIViewController = mainStoryboard.instantiateViewControllerWithIdentifier(viewControllerWithIdentifier) as UIViewController
self.window = UIWindow(frame: UIScreen.main.bounds)
self.window?.rootViewController = initialViewController
self.window?.makeKeyAndVisible()
return true
}

Select storyboard to launch on launchScreen completion

After completion of launchScreeen(Splash) I want to be able to select which storyboard to launch. For example launch login.storyboard if user is not logged in or launch dashboard.storyboard if user is logged in. Currently Main.storyboard is launched after launchScreen. in appdelegate I have a code for checking the login status as follows:
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
// Override point for customization after application launch.
var launchDashBoard = false
let isUserLoggedIn = UserDefaults.standard.object(forKey: TAG_IS_USER_LOGGEDIN) as? Bool
if isUserLoggedIn != nil {
launchDashBoard = isUserLoggedIn!
}
if launchDashBoard {
self.loadDashBoard()
}else{
self.loadIntro()
}
return true
}
func loadHome(){
let storyboard = UIStoryboard(name: "Main", bundle: nil)
let home = storyboard.instantiateViewController(withIdentifier: "dashboard") as! Dashboard
let navigationController = UINavigationController(rootViewController: home)
self.window?.rootViewController = navigationController
}
func loadLogin(_ viewController: UIViewController?){
let storyboard = UIStoryboard(name: "Main", bundle: nil)
let home = storyboard.instantiateViewController(withIdentifier: "signIn") as! SignInVC
home.previousViewController = viewController
let navigationController = UINavigationController(rootViewController: home)
self.window?.rootViewController = navigationController
}
If I run the app with this code it crashes with following log:
Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: 'Pushing a navigation controller is not supported'
*** First throw call stack:
Is there a way to include a function that determines which storyboard to launch on launchScreen loading is finished or is there something wrong with my code?
You can create a new Navigation.storyboard. That storyboard can have an initial view controller(Call it StartUpNavigationViewController), that will be opened when Launch screen has been shown.
In that StartUpNavigationViewController, check for the logic if the user is logged in or not, based on that you can navigate to login or dashboard storyboard.

Present login view controller modally in swift tabbed application

I'm trying to present a view controller modally from my app delegate, however, I'm receiving the below error. When creating my project I chose a tabbed application. I need help resolving this error and presenting the view controller. Would also appreciate code to dismiss the view controller too
2018-10-03 20:28:57.324 App[74397:2825933] Warning: Attempt to present <App.SignUpViewController: 0x7fbd7b608c60> on <UITabBarController: 0x7fbd7b407620> whose view is not in the window hierarchy!
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
// Override point for customization after application launch.
showSignUpView()
return true
}
func showSignUpView() {
let storyboard = UIStoryboard(name: "Main", bundle: nil)
let signUpViewController = storyboard.instantiateViewController(withIdentifier:"SignUpViewController") as! SignUpViewController
signUpViewController.modalPresentationStyle = UIModalPresentationStyle.fullScreen
signUpViewController.modalTransitionStyle = UIModalTransitionStyle.coverVertical
window?.rootViewController?.present(signUpViewController, animated: true, completion: nil)
}

AppDelegate window initialisation does not have correct size

I have an app in Xcode which I want to start without a storyboard, so I created a new window in AppDelegate:
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
let InitialViewController: SignViewController = SignViewController()
let NavigationController: UINavigationController = UINavigationController(rootViewController: InitialViewController)
self.window = UIWindow(frame: UIScreen.main.bounds)
self.window!.rootViewController = NavigationController
self.window!.makeKeyAndVisible()
return true
}
The problem with this is that if I simulate it it returns a weird looking screen which seems to be to small on the y axis, and I don't know why:
Can anyone tell me what I need to change ?
I hand the same problem like you did here. Check your info.plist. Did you delete the "Launch screen interface file base name"? If you did, add it back, and the problem would be solved.

Have ViewController appear only on first launch

I would like to have my viewController named "DoctorInformationController" appear only when the user launches the app for the first time. Ive seen some people's answers, however i would like to know how to do this in swift please!
Thank you
This code I use that will launch a viewcontoller based on first or second launch of the app.
This in the AppDelegate.
var defaults: NSUserDefaults
func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
let firstLaunch = defaults.boolForKey("FirstLaunch")
if firstLaunch {
print("Not first launch.")
self.window = UIWindow(frame: UIScreen.mainScreen().bounds)
let storyboard = UIStoryboard(name: "Main", bundle: nil)
let initialViewController = storyboard.instantiateViewControllerWithIdentifier("navController") as! UINavigationController
self.window?.rootViewController = initialViewController
self.window?.makeKeyAndVisible()
} else {
print("First launch, setting NSUserDefault.")
self.window = UIWindow(frame: UIScreen.mainScreen().bounds)
let storyboard = UIStoryboard(name: "Main", bundle: nil)
let initialViewController = storyboard.instantiateViewControllerWithIdentifier("firstrunVCstoryboard") as UIViewController
self.window?.rootViewController = initialViewController
self.window?.makeKeyAndVisible()
}
return true
}
You would need to adapt it to fit your storyboard and viewcontrollers names.
EDIT:
This how I set the value in the first view controller to indicate that first run has completed. So that it will not show again.
self.defaults.setBool(true, forKey: "FirstLaunch")
There is no way to tell if it's the first launch built into the UIKit framework, however you can do it another way.
In AppDelegate.swift, go to applicationDidFinishLaunchingWithOptions method (the first one, usually). Now, add this:
//Check for key "first_launch" in User Defaults
if let _ = NSUserDefaults.standardUserDefaults().objectForKey("first_launch") {
//Set your own global variable to be true. Then, when your ViewController
//loads, do a popup window with that DoctorViewController thingy if the
//variable is true
//Example:
isFirstLaunch = true
//Then, set "first_launch" to be a value so your app will never call this block again
NSUserDefaults.standardUserDefaults().setObject("", forKey: "first_launch")
}