start contentview from appdelegate - ios

Pls, would you advise?
I am testing some old pod and I am getting "UIAlertView is deprecated and unavailable for UIScene based applications, please use UIAlertController"
I googled somewhere to remove scene delegate, comment out UISceneSession, etc..
I did all the steps and when I start the app the contentview is always black :(
Pls, how to instantiate the Content view from appdelegate?

Try this:
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool
{
let timeline = YourViewController()
let navigation = UINavigationController(rootViewController: timeline)
let frame = UIScreen.main.bounds
window = UIWindow(frame: frame)
window!.rootViewController = navigation
window!.makeKeyAndVisible()
return true
}

The SceneDelegate.swift doesn't have to be commented, there are some steps you have to be aware of.
Set a window instance to your AppDelegate window property, as #pkc456 recommended.
window = UIWindow(frame: UIScreen.main.bounds)
Remove the Scene configuration from the AppDelegate (You have already done it by commenting it as showed on your screenshot).
Delete the Scene configuration key from Info.plist file.
Info.plist > Application Scene Manifest > Scene Configuration
Remember to use the View Hierarchy debugger, sometimes you are seeing the correct view controller but it doesn't have a backgroundColor.
Hope I Helped!

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:

Change background color without storyboard and viewController in Xcode 8.2.1

I took the one month iOS course and found that the content was a bit outdated because of the changes in Xcode. In lesson 4, the teacher taught us to change the background color in appDelegate.swift by first deleting the storyboard and viewController as well as eliminating the "main" tab in Info.plist. And then he typed in codes in appDelegate.swift:
func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
// Override point for customization after application launch.
self.window = UIWindow(frame: UIScreen.mainScreen().bounds)
self.window?.backgroundColor = UIColor.magentaColor()
self.window?.makeKeyAndVisible()
return true
}
But the syntax is little bit different in Xcode8.2.1, so i modified it to the code as below. But it doesn't work either.
Then i found that there were updated codes on Github for this course.
Unfortunately, it did't work either( but I know it's because it did not define "StaffPicksViewController").
So what should i do to change the background color without storyboard and viewController?
I received a great answer from G+:
self.window = UIWindow(frame: UIScreen.main.bounds)
self.window?.rootViewController = UIViewController()
self.window?.backgroundColor = UIColor.magenta
self.window?.makeKeyAndVisible()
I just forgot to init a rootViewController...

Resources