Setting Up CoreData with SceneDelegate - unknown identifier 'window' error - iOS 13 onwards

微笑、不失礼 提交于 2020-05-29 11:57:57

问题


I was trying to use the official apple documentation for Core Data. Found here. I also ran into a question which was related to my issue, right here on stack.

I ran into an issue where, it kept saying that 'window' is not available in the context of AppDelegate. This is the very basic step as per the official documentation.

    func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {        
    if let rootVC = window?.rootViewController as? ViewController {
        rootVC.container = persistentContainer
    }        
    return true
}

How do I get past this?


回答1:


The issue boils down to the changes, primarily, support for multiple scenes in iOS 13 and above. check this reddit link for the discussion.

The solution is to move some of things from AppDelegate to SceneDelegate.

Here is the final form of the essential parts of the above two classes.

----SceneDelegate

    func scene(_ scene: UIScene, willConnectTo session: UISceneSession, options connectionOptions: UIScene.ConnectionOptions) {
    guard let windowScene = (scene as? UIWindowScene) else { return }
    self.window = UIWindow(windowScene: windowScene)
    let storyboard = UIStoryboard(name: "Main", bundle: nil)
    guard let rootVC = storyboard.instantiateViewController(identifier: "ViewController") as? ViewController else {
        print("ViewController not found")
        return
    }
    //set the storage here
    rootVC.container = (UIApplication.shared.delegate as? AppDelegate)?.persistentContainer
    //I dont want a  UI navigation controller.
    //let rootNC = UINavigationController(rootViewController: rootVC)
    //self.window?.rootViewController = rootNC
    //I want to use my basic view controller here. use rootNC to get a UI navigation controller
    self.window?.rootViewController = rootVC
    self.window?.makeKeyAndVisible()

}

--AppDelegate (remains unchanged )

func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
    // Override point for customization after application launch.
    //before iOS 13 you would be putting stuff here but not anymore.
    return true
}

Finally, you will leave the storage related code, where it was, in AppDelegate itself.



来源:https://stackoverflow.com/questions/60421107/setting-up-coredata-with-scenedelegate-unknown-identifier-window-error-ios

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!