We can't find the internet
Attempting to reconnect
For the new iOS app for Airlist, I’m building it in (as much as I can) SwiftUI. For anyone coming from React (or in our case, we use Vue to build the web app), SwiftUI is a welcome change vs UIKit. Yet, at times can be frustrating. Often times problems are not documented, and not available via a Google Search.
Along the way I’ve had to solve many problems that, for anyone else building a real-world SwiftUI app, might find helpful. I’ll be documenting these solutions on this blog. They’ve been helpful to me, hopefully they are to you.
Adding back scene delegate
Starting with Swift 2.0 you can now build apps in 100% SwiftUI, removing the need for AppDelegate
. If you need, /It’s easy to add back the standard AppDelegate functions./
But what about SceneDelegate, or any of it’s functions? (windowScene: UIWindowScene, performActionFor
or scene: UIScene, willConnectTo)
There is literally 0 documentation. After spending way to long on this, I began thinking adding back SceneDelegate was not possible.
But it is. Check out below.
In your class AppDelegate:...
(described here), add the following:
func application(_ application: UIApplication, configurationForConnecting connectingSceneSession: UISceneSession, options: UIScene.ConnectionOptions) -> UISceneConfiguration {
let sceneConfiguration = UISceneConfiguration(name: "My Config", sessionRole: connectingSceneSession.role)
sceneConfiguration.delegateClass = CustomSceneDelegate.self
return sceneConfiguration
}
Then, outside your class AppDelegate:...
add:
class CustomSceneDelegate: UIResponder, UIWindowSceneDelegate {
@Environment(\.openURL) var openURL...
}
Viola! SceneDelegate is hooked up and ready to go.
Now why do you need this?
Let’s say you want to be able to handle shortcutItems.
i.e. spotlight search, x-callback urls… Using .onOpenURL
will work. But it will not work for Quick Actions (3D touching the app icon) from a cold start. Only scene: UIScene, willConnectTo session: UISceneSession
will. Like so:
func scene(_ scene: UIScene, willConnectTo session: UISceneSession, options connectionOptions: UIScene.ConnectionOptions) {
/** Process quick action shortcut if opened from a cold start **/
if let shortcutItem = connectionOptions.shortcutItem {
if let id = shortcutItem.userInfo?["id"] {
// do stuff here...
}
}
}
We hate spam too. You’ll only receive periodic important Airlist information. Unsubscribe anytime.
We’re also posting regular and in-depth updates on twitter.