Skip to content

Instantly share code, notes, and snippets.

@aheze
Last active June 30, 2020 18:02
Show Gist options
  • Select an option

  • Save aheze/d1c5455c5e0ba5531bbe9d4e9a9e6560 to your computer and use it in GitHub Desktop.

Select an option

Save aheze/d1c5455c5e0ba5531bbe9d4e9a9e6560 to your computer and use it in GitHub Desktop.
import SwiftUI
class AppDelegate: NSObject, UIApplicationDelegate {
@Published var shortcutItemType: String?
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey : Any]? = nil) -> Bool {
if let shortcutItem = launchOptions?[.shortcutItem] as? UIApplicationShortcutItem {
if shortcutItem.type == "com.example.ExampleApp.exampleAction" {
shortcutItemType = shortcutItem.type
}
}
return true
}
}
@main
struct SwiftUI_AppApp: App {
@UIApplicationDelegateAdaptor(AppDelegate.self) var appDelegate
@State var shortcutPressed = false
var body: some Scene {
WindowGroup {
ContentView(shortcutPressed: $shortcutPressed)
.onReceive(appDelegate.$shortcutItemType) { _ in
shortcutPressed = true
}
}
}
}
struct ContentView: View {
@Binding var shortcutPressed: Bool
let defaults = UserDefaults.standard
var body: some View {
if shortcutPressed {
return Text("A quick action was tapped!")
} else {
return Text("Just a normal SwiftUI View!")
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment