I would use NotificationCenter. Let's say your app naturally launches to HomeViewController, which happens to be the first VC of the main tab bar controller, and that you want to define "New Post" shortcut item so that as soon as HomeViewController is loaded it performs segue to NewPostViewController.
First, define notification name:
extension Notification.Name {
Notification.Name("applicationLaunchedWithNewPostShortcutItem")
}
Second, post notification when the app is launched with the shortcut item (I'm assuming it's the first static shortcut item):
func handleShortCutItem(_ shortcutItem: UIApplicationShortcutItem) -> Bool {
var handled = false
guard ShortcutIdentifier(fullType: shortcutItem.type) != nil else { return false }
guard let shortCutType = shortcutItem.type as String? else { return false }
switch shortCutType {
case ShortcutIdentifier.first.type:
NotificationCenter.default.post(name: .applicationLaunchedWithNewPostShortcutItem, object: nil, userInfo: nil)
handled = true
break
default:
break
}
return handled
}
For those who are not familiar with above method, it is usually defined in your AppDelegate.swift and called in application(_:performActionFor:completionHandler:), also in AppDelegate.swift. See Apple's sample code here.
Lastly, allow HomeViewController to be able to tune-in to the notification by implemening addObservers() and removeObservers() method in it.
class HomeViewController {
// MARK: - View Controller Life Cycle
override func viewDidLoad() { // or viewWillAppear(), etc.
super.viewDidLoad()
NotificationCenter.default.addObserver(forName: .applicationLaunchedWithNewPostShortcutItem, object: nil, queue: .main) { [weak self] (notification) in
self?.handleApplicationLaunchedWithNewPostShortcutItem(notification: notification)
}
}
deinit { // or viewWillDisappear(), etc.
removeObservers()
}
// MARK: - Notification Center Handlers
private func handleApplicationLaunchedWithNewPostShortcutItem(notification: Notification) {
performSegue(withIdentifier: "presentNewPost", sender: nil)
}