StalicJi
StalicJi

Reputation: 1

Black screen in run application

enter image description here

I'm trying to do a new iOS app in Xcode. I made a main storyboard and I added a label on my ViewController. When I run my application, first second it show the label and then become the screen black without any errors.

I'm working on Xcode 14

I tried to add (var window: UIWindow?) in my AppDelegate, but the app still crashed .

here is my code, please help me :(

AppDelegate

import UIKit

import CoreData

@UIApplicationMain

class AppDelegate: UIResponder, UIApplicationDelegate {


func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
  
    return true
}


func applicationWillTerminate(_ application: UIApplication) {
    self.saveContext()
}



// MARK: - Core Data stack

lazy var persistentContainer: NSPersistentContainer = {
    let container = NSPersistentContainer(name: "DataModel")
    container.loadPersistentStores(completionHandler: { (storeDescription, error) in
        if let error = error as NSError? {
 
            fatalError("Unresolved error \(error), \(error.userInfo)")
        }
    })
    return container
}()

// MARK: - Core Data Saving support

func saveContext () {
    let context = persistentContainer.viewContext
    if context.hasChanges {
        do {
            try context.save()
        } catch {
           
            let nserror = error as NSError
            fatalError("Unresolved error \(nserror), \(nserror.userInfo)")
        }
    }
}

}

SceneDelegate

import UIKit


class SceneDelegate: UIResponder, UIWindowSceneDelegate {

var window: UIWindow?


func scene(_ scene: UIScene, willConnectTo session: UISceneSession, options connectionOptions: UIScene.ConnectionOptions) {
  
    
    guard let _ = (scene as? UIWindowScene) else { return }
    

}

func sceneDidDisconnect(_ scene: UIScene) {

}

func sceneDidBecomeActive(_ scene: UIScene) {

}

func sceneWillResignActive(_ scene: UIScene) {

}

func sceneWillEnterForeground(_ scene: UIScene) {

}

func sceneDidEnterBackground(_ scene: UIScene) {

    (UIApplication.shared.delegate as? AppDelegate)?.saveContext()
}


}

Upvotes: 0

Views: 125

Answers (1)

TienTranMinh
TienTranMinh

Reputation: 182

try to add

var window: UIWindow?

into your app delegate

Upvotes: 0

Related Questions