Migrating a Single-View Objective-C Application to the UIKit Scene-Based Life Cycle

Hello! I've been using Objective-C to write a single-view application and have been using AppDelegate for my window setup.

However, with the next release of iOS, I need to adopt SceneDelegate into my app for it to work.

I'm having trouble getting my main ViewController to show up when the app starts. It just shows up as a black screen.

I built this app entirely programmatically. When I go into my info.plist file, I have everything but the Storyboard setup since, obviously, I don't have one.

I've left my code below.

Thank you!

My current AppDelegate:

#import "ViewController.h"

@interface AppDelegate ()

@end

@implementation AppDelegate


#pragma mark - UISceneSession Configuration

- (UISceneConfiguration *)application:(UIApplication *)application configurationForConnectingSceneSession:(UISceneSession *)connectingSceneSession options:(UISceneConnectionOptions *)options {
    
    // Create the configuration object
    return [[UISceneConfiguration alloc] initWithName: @"Default" sessionRole: connectingSceneSession.role];
}

#pragma mark - Application Configuration

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    return YES;
}

- (void) dealloc {
    [super dealloc];
}

@end

My Current SceneDelegate:

#import "SceneDelegate.h"
#import "ViewController.h"

@interface SceneDelegate ()

@end

@implementation SceneDelegate

- (void)scene:(UIScene *)scene willConnectToSession:(UISceneSession *)session options:(UISceneConnectionOptions *)connectionOptions {
    // Ensure we have a UIWindowScene
    if (![scene isKindOfClass:[UIWindowScene class]]) {
        return;
    }
    UIWindowScene *windowScene = (UIWindowScene *)scene;

    // Create and attach the window to the provided windowScene
    self.window = [[UIWindow alloc] initWithWindowScene:windowScene];

    // Set up the root view controller
    ViewController *homeViewController = [[[ViewController alloc] init] autorelease];
    UINavigationController *navigationController = [[[UINavigationController alloc] initWithRootViewController: homeViewController] autorelease];

    // Present the window
    self.window.rootViewController = navigationController;
    [self.window makeKeyAndVisible];
}

@end
Answered by dteakell in 862996022

After messing around with my info.plist and file structure, putting the SceneDelegate.h and SceneDelegate.m files in the root directory resolved the issue.

  • If you're having issues with either the AppDelegate or SceneDelegate, try moving them to the root.

A black screen when launching your app typically indicates that your app has no UIWindows. Your code does create a window and it does set it up correctly, but it then assigns it to a self.window variable on the SceneDelegate. Where is that window property declared? Is it a strong or weak property? It's likely that you're storing it in a weak property, so it gets deallocated immediately.

Accepted Answer

After messing around with my info.plist and file structure, putting the SceneDelegate.h and SceneDelegate.m files in the root directory resolved the issue.

  • If you're having issues with either the AppDelegate or SceneDelegate, try moving them to the root.
Migrating a Single-View Objective-C Application to the UIKit Scene-Based Life Cycle
 
 
Q