'iOS Convering an Old project to use UI

I am trying to convert (slowly mordenize) an old iOS project to support UISceneDelegate. However at the moment the old appdelegate creates a UIWindow and attaches a UIViewController to the windows rootViewController in the finishedLaunching.

As I am not ready to convert my application to Storyboards yet is there a way I can achieve the same result using UISceneDelegate ?

Please note the mainViewController is not backed by a XIB or a storyboard so there is no reference to any UI Files in the UIWindowSceneSessionRoleApplication portion of my info.plist

Regards Christian Stœr Andersen



Solution 1:[1]

Implement the following method in your UIWindowSceneDelegate subclass to assign your UIViewController:

func scene(_ scene: UIScene, willConnectTo session: UISceneSession, options connectionOptions: UIScene.ConnectionOptions) {
   guard let windowScene = scene as? UIWindowScene else {return}
   self.window = UIWindow(windowScene: windowScene)
   self.window!.rootViewController = YourViewController() // Instantiate your UIViewController 
   self.window!.makeKeyAndVisible()
}

With objective-c:

- (void)scene:(UIScene *)scene willConnectToSession:(UISceneSession *)session options:(UISceneConnectionOptions *)connectionOptions {
   self.window = [[UIWindow alloc] initWithWindowScene:(UIWindowScene *)scene];
   self.window.rootViewController = [[YourViewController alloc] init];
   [self.window makeKeyAndVisible];
}

Solution 2:[2]

If you're developing Xamairn.iOS, here is the sample.

Remain the code in AppDelegate , and add the following code into SceneDelegate .

[Export ("scene:willConnectToSession:options:")]
public void WillConnect (UIScene scene, UISceneSession session, UISceneConnectionOptions connectionOptions)
{
    UIWindowScene windowScene = scene as UIWindowScene;
    Window = new UIWindow(windowScene);
    Window.Frame = windowScene.CoordinateSpace.Bounds;
    Window.RootViewController = new YourViewController();
    Window.MakeKeyAndVisible ();            
}

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 RedWheelbarrow
Solution 2 ColeX - MSFT