Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to open to a specific view using home quick actions

I am new to Swift and have been using SwiftUI, not Storyboard.

I set the UIApplicationShortcutItems in the Info.plist and have two quick actions that are able to present an alert with launchOptions.

I am able to switch-case out the quick actions in SceneDelegate.swift

func windowScene(_ windowScene: UIWindowScene, performActionFor shortcutItem: UIApplicationShortcutItem, completionHandler: @escaping (Bool) -> Void) {
    switch shortcutItem.type {
    case "QuickAction1":
        OneView() // What do I do to open this SwiftUI struct View?
        break
    case "QuickAction2":
        SecondView() // What do I do to open this SwiftUI struct View?
        break
    default:
        break
    }
}

What is the proper way to open a particular view from a home quick action using SwiftUI?


ContentView.swift

struct ContentView: View {
    var body: some View {
        NavigationView {
            VStack {
                NavigationLink(destination: OneView())
                NavigationLink(destination: TwoView())
            }
        }
    }
}
like image 561
user1909186 Avatar asked Jan 10 '20 07:01

user1909186


People also ask

How do you set up quick actions on Iphone?

On the Home Screen and in App Library, touch and hold apps to open quick actions menus. For example: Touch and hold Camera , then choose Take Selfie. Touch and hold Maps , then choose Send My Location.

What is quick action in iOS shortcuts?

Quick actions are a great way to provide your users fast access to your app's common functionality within the home screen. iOS 13 introduced the concept of quick actions, where a user can touch and hold an app icon to display a set of shortcuts or actions to perform right from the home screen.

What is the use of quick actions?

With custom quick actions, you can make your users' navigation and workflow as smooth as possible by giving them convenient access to information that's most important. For example, you can let users create or update records and log calls directly in their Chatter feed or from their mobile device.


1 Answers

Here is possible approach by steps (tested with Xcode 11.2 / iOS 13.2)

  1. Add AppSettings class to store mode of views to be presented by shorcut
    class AppSettings: ObservableObject {
        enum Mode {
            case one
            case two
        }
        @Published var mode: Mode? = nil
    }
  1. make appSettings scene delegate member to access it and in ContentView and in shortcut delegate and pass it in ContentView as environment object
    let appSettings = AppSettings()
    
    func scene(_ scene: UIScene, willConnectTo session: UISceneSession, options connectionOptions: UIScene.ConnectionOptions) {
    
        let contentView = ContentView().environmentObject(appSettings)
        // ...
  1. activate corresponding mode in shortcut scene delegate
    func windowScene(_ windowScene: UIWindowScene, performActionFor shortcutItem: UIApplicationShortcutItem, completionHandler: @escaping (Bool) -> Void) {
        switch shortcutItem.type {
        case "QuickAction1":
            appSettings.mode = .one
            break
        case "QuickAction2":
            appSettings.mode = .two
            break
        default:
            break
        }
        // ...
    }
  1. Make ContentView present links conditionally based on selected shortcut mode
    struct ContentView: View {
        @EnvironmentObject var appSettings: AppSettings
        
        var body: some View {
            NavigationView {
                VStack {
                    NavigationLink(destination: OneView(),
                                   tag: AppSettings.Mode.one,
                                   selection: $appSettings.mode)
                        { Text("To One") }
                    NavigationLink(destination: TwoView(),
                                    tag: AppSettings.Mode.two,
                                    selection: $appSettings.mode)
                        { Text("To Two") }
                }
            }
        }
    }

backup

like image 176
Asperi Avatar answered Sep 28 '22 15:09

Asperi