DEV Community

Add a Scene Delegate to your current project

Maarek on October 28, 2019

With iOS 13's introduced UIWindowScene and multiple window support for iPad OS, you might be looking to add Scene Delegate to your existing app (if...
Collapse
 
dkweb profile image
DKWeb

Hi Maarek, thanks for this great tutorial. But i have a question. I have a storyboard (uikit) App, and i want to use coredata in swiftui views. For this i need a scenedelegate. Now i have used your tutorial and leave the scene method empty. Do I have to add the manifest anyway? I added my main storyboard but only got a black screen. Even after I emptied the build. Can you help me? Thank you so much!

Collapse
 
kevinmaarek profile image
Maarek

The manifest si here to declare the scene delegate you set up (even if it contains empty methods) as being your scene delegate, the one the app will use.

If you're using storyboards, you don't to fill up the scene delegate methods. Just set the storyboard name in the plist.
So yes, you need to add a manifest in your plist file.
You info.plist should look like this (assuming the name of your storyboard is "Main") :
info.plist

You shouldn't have black screen if you have your storyboard set up and declared in these (both) plist variables.

Collapse
 
dkweb profile image
DKWeb

Hi Maarek,

Thank you for your quick response. I have the plist identical to yours. In the SceneDelegate I have the func scene empty. Still, I only get one black screen. What am I doing wrong?

Thread Thread
 
kevinmaarek profile image
Maarek

Does your storyboard has an initial view controller set (with an arrow on the left) ?

Thread Thread
 
dkweb profile image
DKWeb • Edited

Hi Maarek,

Yes I have activated an initial view controller. Do not understand why the screen stays black. As soon as I remove the manifest, the app goes back. Do you have any idea?

thepracticaldev.s3.amazonaws.com/i...
thepracticaldev.s3.amazonaws.com/i...
thepracticaldev.s3.amazonaws.com/i...

Thread Thread
 
dkweb profile image
DKWeb

I Hi Maarek,

I am now a little closer to the goal. I had to paste this code into the scene delegate:

Β Β Β Β Β Β Β Β  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

Now the ViewController is displayed, but everything is crashed. TableView (found nil), present other view controller by button crash. What is going on here?

Thread Thread
 
jacklink01 profile image
Jack Klink

Hey! Have you had any resolution to this? I am also experiencing this issue of a black screen while using a Main storyboard.

Thread Thread
 
xorforce profile image
Bhagat Singh • Edited

Hi. I guess I figured it out. The

<key> Storyboard Name </key>

is wrong. It should be

<key> UISceneStoryboardFile </key>

. It made it work for me.

Thread Thread
 
leandrooodesousa profile image
Leandro de Sousa

It does not work for me! somebody help me, please.

Collapse
 
gerkov77 profile image
gerkov77

Great tut, thanks!

Collapse
 
rukmanary profile image
Ryandhika Rukmana • Edited

Hi Maarek, can you show me how it can be done in Objective-C? I'm working on React Native Project. Thank you

Collapse
 
kevinmaarek profile image
Maarek

Hi,
SceneDelegate is just an implementation of a UIWindowSceneDelegate and UIResponder.
I know nothing about React Native, but as long as you have an App target and an info.plist file, you must be able to create an Objective-C class called SceneDelegate conforming to these and declare it in your info.plist.

Collapse
 
andrey_torlopov_769565d20 profile image
Andrey Torlopov

And what about iOS 15? How I can get current Scene or Window in xCode 13 beta and iOS 15?