Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

@intan1907
Created June 16, 2021 09:19
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save intan1907/6fbc0251675aaa01e7d42f2c215cb575 to your computer and use it in GitHub Desktop.
Save intan1907/6fbc0251675aaa01e7d42f2c215cb575 to your computer and use it in GitHub Desktop.
import UIKit
import SwiftUI
import HeartyRecipeHelper
class SceneDelegate: UIResponder, UIWindowSceneDelegate {
// ...
func scene(_ scene: UIScene, willConnectTo session: UISceneSession, options connectionOptions: UIScene.ConnectionOptions) {
// ...
DispatchQueue.main.asyncAfter(deadline: .now() + 1) { [weak self] in
guard let self = self else { return }
if let url = connectionOptions.urlContexts.first?.url {
self.handleURL(url: url)
}
}
}
// ...
func scene(_ scene: UIScene, openURLContexts URLContexts: Set<UIOpenURLContext>) {
guard let url = URLContexts.first?.url else {
return NSLog("No URL passed to open the app")
}
handleURL(url: url)
}
}
extension SceneDelegate {
func handleURL(url: URL) {
guard let recipe = recipeData?.first(where: { $0.widgetURL == url }) else {
let message = "Sorry, we can't find the recipe named \'\(url.host?.removingPercentEncoding ?? "")\'"
let alertController = UIAlertController(title: "Incoming Message", message: message, preferredStyle: .alert)
let okAction = UIAlertAction(title: "OK", style: UIAlertAction.Style.default, handler: nil)
alertController.addAction(okAction)
window?.rootViewController?.present(alertController, animated: true, completion: nil)
return
}
if let nc = window?.rootViewController?.children.first?.children.first as? UINavigationController {
// if the view currently being displayed is DetailRecipeView, go back to the root view controller
if nc.title == "Detail Recipe" {
nc.popToRootViewController(animated: true)
}
// push the DetailRecipeView
nc.pushViewController(HostingController(rootView: DetailRecipeView(recipe: recipe)), animated: true)
} else {
window?.rootViewController?.present(HostingController(rootView: DetailRecipeView(recipe: recipe)), animated: true, completion: nil)
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment