Black screen after adding SceneDelegate and updating Info.plist
You have several issues here. It's important to read the documentation related to the app lifecycle which states what is called under iOS 13 and what is called under iOS 12.
You may also want to see my Single View App template that supports iOS 12 and 13.
Looking at your code, here is a summary of the problems:
AppDelegate:
- You should only setup the main window and the root view controller if the app is being run under iOS 12 or earlier. You need to check this at runtime.
- The
func scene(_ scene: UIScene, willConnectTo session: UISceneSession, options connectionOptions: UIScene.ConnectionOptions)
method should not be in the app delegate. - Not directly related but never sleep on app startup. Remove the
Thread.sleep(forTimeInterval: 3.0)
line. Users want to use your app, not stare at the launch screen longer than necessary. And blocking the main thread on app launch can cause your app to be killed.
SceneDelegate:
- This is mostly fine but there is no reason for the
guard let _ = (scene as? UIWindowScene) else { return }
line, especially since it is inside anif let
that already does that check. - You don't appear to be using SwiftUI so remove that import.
I would update your app delegate to be more like this:
@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate {
var window: UIWindow?
func application(_ application: UIApplication, willFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]? = nil) -> Bool {
UINavigationBar.appearance().barTintColor = UIColor(red:0.08, green:0.23, blue:0.62, alpha:1.0)
if #available(iOS 13.0, *) {
// In iOS 13 setup is done in SceneDelegate
} else {
let window = UIWindow(frame: UIScreen.main.bounds)
self.window = window
if (user != nil && userSelfIdent != nil){
let mainstoryboard:UIStoryboard = UIStoryboard(name: "Main", bundle: nil)
let newViewcontroller:UIViewController = mainstoryboard.instantiateViewController(withIdentifier: "swrevealviewcontroller") as! SWRevealViewController
window.rootViewController = newViewcontroller
}
}
return true
}
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
if #available(iOS 13.0, *) {
// In iOS 13 setup is done in SceneDelegate
} else {
self.window?.makeKeyAndVisible()
}
return true
}
func applicationWillResignActive(_ application: UIApplication) {
// Not called under iOS 13 - See SceneDelegate sceneWillResignActive
}
func applicationDidEnterBackground(_ application: UIApplication) {
// Not called under iOS 13 - See SceneDelegate sceneDidEnterBackground
}
func applicationWillEnterForeground(_ application: UIApplication) {
// Not called under iOS 13 - See SceneDelegate sceneWillEnterForeground
}
func applicationDidBecomeActive(_ application: UIApplication) {
// Not called under iOS 13 - See SceneDelegate sceneDidBecomeActive
}
// MARK: UISceneSession Lifecycle
@available(iOS 13.0, *)
func application(_ application: UIApplication, configurationForConnecting connectingSceneSession: UISceneSession, options: UIScene.ConnectionOptions) -> UISceneConfiguration {
// Called when a new scene session is being created.
// Use this method to select a configuration to create the new scene with.
return UISceneConfiguration(name: "Default Configuration", sessionRole: connectingSceneSession.role)
}
@available(iOS 13.0, *)
func application(_ application: UIApplication, didDiscardSceneSessions sceneSessions: Set<UISceneSession>) {
// Called when the user discards a scene session.
// If any sessions were discarded while the application was not running, this will be called shortly after application:didFinishLaunchingWithOptions.
// Use this method to release any resources that were specific to the discarded scenes, as they will not return.
}
}
Your scene delegate could be like:
@available(iOS 13.0, *)
class SceneDelegate: UIResponder, UIWindowSceneDelegate {
var window: UIWindow?
func scene(_ scene: UIScene, willConnectTo session: UISceneSession, options connectionOptions: UIScene.ConnectionOptions) {
guard let windowScene = (scene as? UIWindowScene) else { return }
let window = UIWindow(windowScene: windowScene)
self.window = window
if (user != nil && userSelfIdent != nil){
let mainstoryboard:UIStoryboard = UIStoryboard(name: "Main", bundle: nil)
let newViewcontroller:UIViewController = mainstoryboard.instantiateViewController(withIdentifier: "swrevealviewcontroller") as! SWRevealViewController
window.rootViewController = newViewcontroller
window.makeKeyAndVisible()
}
}
func sceneDidDisconnect(_ scene: UIScene) {
// Called as the scene is being released by the system.
}
func sceneDidBecomeActive(_ scene: UIScene) {
// Not called under iOS 12 - See AppDelegate applicationDidBecomeActive
}
func sceneWillResignActive(_ scene: UIScene) {
// Not called under iOS 12 - See AppDelegate applicationWillResignActive
}
func sceneWillEnterForeground(_ scene: UIScene) {
// Not called under iOS 12 - See AppDelegate applicationWillEnterForeground
}
func sceneDidEnterBackground(_ scene: UIScene) {
// Not called under iOS 12 - See AppDelegate applicationDidEnterBackground
}
}
black screen with sceneDelegate when using storyboard
EDIT: You need to instantiate from storyboard since you are using storyboard.
You need to initialize your window variable window with windowScene.
var window: UIWindow?
func scene(_ scene: UIScene, willConnectTo session: UISceneSession, options connectionOptions: UIScene.ConnectionOptions) {
guard let windowScene = (scene as? UIWindowScene) else { return }
window = UIWindow(windowScene: windowScene)
let storyboard = UIStoryboard(name: "Main", bundle: nil)
if let user = Auth.auth().currentUser {
FirestoreServices.shared.getUserData(user: user) { (result) in
switch result {
case .success(let muser):
let mainTabBar = storyboard.instantiateViewController(withIdentifier: "viewControllerIDInStoryboard") as! TabViewController
mainTabBar.currentUser = muser
mainTabBar.modalPresentationStyle = .fullScreen
self.window?.rootViewController = mainTabBar
print("BLA BLA \(String(describing: self.window?.frame))")
case .failure(_):
let authVC = storyboard.instantiateViewController(withIdentifier: "viewControllerIDInStoryboard") as! AuthViewController
self.window?.rootViewController = authVC
}
}
} else {
let authVC = storyboard.instantiateViewController(withIdentifier: "viewControllerIDInStoryboard") as! AuthViewController
self.window?.rootViewController = authVC
}
window?.makeKeyAndVisible()
}
iOS : Black screen appears when run my app
Step 1:-
you need to remove the dependency of scene delegate from info.plist
(follow Step show in above Ans of Dipak Ramoliya)
Step 2:-
comment code in scene delegate file
step 3:-
comment code related to scene delegate in App delegate file (connectingSceneSession method and sceneSessions method)
step 4:- Add window variable in AppDelegate file (var window: UIWindow?)
step 5:- create rootViewController in didFinishLaunchingWithOptions method in App Delegate
ex:-
let appDelegate = UIApplication.shared.delegate as! AppDelegate
var rootVC = UIStoryboard(name: "Main", bundle: nil).instantiateViewController(withIdentifier: "testController") as! TestController
appDelegate.window?.rootViewController = testController
Application black screen on iOS 13 on launch
The problem is this pair of lines in the Info.plist:
<key>UISceneDelegateClassName</key>
<string>SceneDelegate</string>
The issue is that you think "SceneDelegate" is the name of the SceneDelegate class, but Objective-C does not think so! You have to write:
<key>UISceneDelegateClassName</key>
<string>$(PRODUCT_MODULE_NAME).SceneDelegate</string>
Why is manually setup root view controller showing black screen?
To ensure you see your root view controller in iOS 13 when everything is done programmatically, you must do the following:
In the scene delegate, you must create the window instance and the root view controller:
class SceneDelegate: UIResponder, UIWindowSceneDelegate {
var window: UIWindow?
func scene(_ scene: UIScene, willConnectTo session: UISceneSession, options connectionOptions: UIScene.ConnectionOptions) {
guard let winScene = (scene as? UIWindowScene) else { return }
// Create the root view controller as needed
let vc = ViewController()
let nc = UINavigationController(rootViewController: vc)
// Create the window. Be sure to use this initializer and not the frame one.
let win = UIWindow(windowScene: winScene)
win.rootViewController = nc
win.makeKeyAndVisible()
window = win
}
}
Your Info.plist has to have the "Application Scene Manifest" entry. Below it should be the "Enable Multiple Windows" entry. Set to YES or NO as appropriate to your app. Optionally you should also have the "Scene Configuration" entry.
All of these entries are added by Xcode when you check the "Supports multiple windows" setting on the General tab of your target. This will default the "Enable Multiple Windows" entry to YES so you can change that to NO if you want scenes but not multiple windows.
Black when screen when settings initial controller programmatically
The problem is that, in an attempt to make the project stop using the scene delegate and use the app delegate instead, you mangled the UIApplicationSceneManifest
entry in the Info.plist. Instead, you would need to delete that entry entirely. Its mere presence is what causes the scene delegate architecture to be used.
It would be better, however, to make this work for both iOS 12 using an app delegate and iOS 13 using a scene delegate (as I have described at https://stackoverflow.com/a/58405507/341994).
Why is my Xcode simulator just showing me a black screen? No scene delegate set?
It's the é
in the name of your project. Don't do that.
Keep in mind that the project name is not user-facing. You can call the app by a name that has an é
in it, but don't attempt to start a project with a name like that. Bad Things will happen (and you just encountered one of them).
I suggest that you just start all over. That will be easier than trying to rename the project and all that relates to it.
Related Topics
Is a Date in Same Week, Month, Year of Another Date in Swift
iOS Enterprise Ota Distribution Unable to Download Application
How to Set Device (Ui) Orientation Programmatically
Button in Uitableviewcell Not Responding Under iOS 7
Changed Project Name in Xcode Causing Naming Error
Wkwebview in Interface Builder
Xcode - Error Creating Lldb Target
Module Compiled with Swift 4.2.1 Cannot Be Imported by the Swift 5.0 Compiler
iOS Game Center: Scores Not Showing on Leaderboard in Sandbox
How to Embed a Youtube Video into My App
How to Detect Apps First Launch in iOS
Checking for Multiple Asynchronous Responses from Alamofire and Swift
How to Change the Background Color of the Uialertcontroller