How to resolve: "keyWindow" was deprecated in iOS 13.0

Cover Image for How to resolve: "keyWindow" was deprecated in iOS 13.0
Matheus Mello
Matheus Mello
published a few days ago. updated a few hours ago

How to Resolve: 😱 'keyWindow' was deprecated in iOS 13.0

If you are an iOS developer working with Core Data and Cloud Kit, you may have encountered the dreaded deprecation warning when using UIApplication.shared.keyWindow?.rootViewController?.present(...) in Xcode 11 beta 4. Fear not, we've got you covered! In this guide, we'll walk you through resolving this issue and present an alternative solution. Let's dive in! 🏊‍♂️

Understanding the Deprecation Warning

In Xcode 11 beta 4, Apple introduced a deprecation warning for using keyWindow in iOS 13.0 and later. The deprecation message states that keyWindow should not be used for applications that support multiple scenes, as it returns a key window across all connected scenes. 😬

The Solution: 💡 UIWindowScene

To address this deprecation warning, Apple introduced the concept of UIWindowScene to manage windows within an application that supports multiple scenes. By leveraging this new feature, you can easily present a dialog to the user without relying on the deprecated keyWindow.

Here's how you can update your code to make it compatible with iOS 13.0 and later:

// Check if there's an active scene
if let windowScene = UIApplication.shared.connectedScenes.first as? UIWindowScene {
    // Get the key window associated with the scene
    if let window = windowScene.windows.first {
        // Get the root view controller and present the dialog
        window.rootViewController?.present(...)
    }
}

By using UIApplication.shared.connectedScenes.first as? UIWindowScene, you can obtain the active window scene. Then, accessing windowScene.windows.first retrieves the associated key window within that scene. Finally, you can retrieve the root view controller from the window and present your dialog as before. 🎉

Wrap Up and Engage!

Congratulations on resolving the deprecation warning and updating your code to support multiple scenes! 👏 In case you encounter any issues or have further questions, feel free to leave us a comment below. We'd love to hear from you and help you out.

Don't forget to share this post with your fellow iOS developers who might be facing the same deprecation warning. Together, we can stay up-to-date with the latest iOS developments and build amazing apps. Happy coding! 💻🚀


More Stories

Cover Image for How can I echo a newline in a batch file?

How can I echo a newline in a batch file?

updated a few hours ago
batch-filenewlinewindows

🔥 💻 🆒 Title: "Getting a Fresh Start: How to Echo a Newline in a Batch File" Introduction: Hey there, tech enthusiasts! Have you ever found yourself in a sticky situation with your batch file output? We've got your back! In this exciting blog post, we

Matheus Mello
Matheus Mello
Cover Image for How do I run Redis on Windows?

How do I run Redis on Windows?

updated a few hours ago
rediswindows

# Running Redis on Windows: Easy Solutions for Redis Enthusiasts! 🚀 Redis is a powerful and popular in-memory data structure store that offers blazing-fast performance and versatility. However, if you're a Windows user, you might have stumbled upon the c

Matheus Mello
Matheus Mello
Cover Image for Best way to strip punctuation from a string

Best way to strip punctuation from a string

updated a few hours ago
punctuationpythonstring

# The Art of Stripping Punctuation: Simplifying Your Strings 💥✂️ Are you tired of dealing with pesky punctuation marks that cause chaos in your strings? Have no fear, for we have a solution that will strip those buggers away and leave your texts clean an

Matheus Mello
Matheus Mello
Cover Image for Purge or recreate a Ruby on Rails database

Purge or recreate a Ruby on Rails database

updated a few hours ago
rakeruby-on-railsruby-on-rails-3

# Purge or Recreate a Ruby on Rails Database: A Simple Guide 🚀 So, you have a Ruby on Rails database that's full of data, and you're now considering deleting everything and starting from scratch. Should you purge the database or recreate it? 🤔 Well, my

Matheus Mello
Matheus Mello