Fizzix Fizzix - 2 years ago 124
Swift Question

iOS Application function within AppDelegate for when opened without shortcut

I'm using the following function to detect when my app is opened via a force touch shortcut:

@available(iOS 9.0, *)
func application(application: UIApplication, performActionForShortcutItem shortcutItem: UIApplicationShortcutItem, completionHandler: (Bool) -> Void) {
print("doing performActionForShortcutItem")

Is there another way within my
file to detect when the application is opened without a shortcut?

Answer Source

When activated via a shortcut, (your app has already been launched, but is not active) application:performActionForShortcutItem:completionHandler: will be called.

When launched via a shortcut, the launchOptions dictionary from application:didFinishLaunchingWithOptions: will contain a key UIApplicationLaunchOptionsShortcutItemKey.

If that key is not present, your application was not launched via a shortcut.

If you test to see that a value for that key does not exist, you will know that your app was not launched via a shortcut.

In application:didFinishLaunchingWithOptions: you need something like this:

let launchedByShortcut = launchOptions?[UIApplicationLaunchOptionsShortcutItemKey] != nil

if (!launchedByShortcut) {
    // the app wasn't launched via a shortcut
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download