Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

3D Touch Quick actions not working at all

Ive been through all the other questions and I can't figure out what's wrong. I downloaded the sample project from the Apple Developer site Add Home Screen Quick Actions and that works no problem, but when I start a new Xcode project and copy that exactly it doesn't work for me. I must be missing something. At this point I just want it to print in the console saying "shortcut pressed". When I add print("Shortcut pressed") to the Apple project I downloaded it works fine.

At this point I am just trying random things.

I updated my info.plist with an Array and a dictionary and strings and I just copy and past the values as to not make any typo mistakes.

UIApplicationShortcutItems, Item 0, UIApplicationShortcutItemType, UIApplicationShortcutItemIconType, UIApplicationShortcutItemTitle

The shortcut appears when pressing the app icon but it just opens the app.

This is my very basic AppDelegate.Swift file just trying to get it to do anything Could it be my project settings, my version of Xcode is up to date - Version 11.1 (11A1027)

Ive never used Quick actions before and it seamed simple but what seamed simple, just add some lines to the plist and add some code to the AppDelegate.Swift file but is taking ages to get working.

import UIKit

@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate {

     var shortcutItemToProcess: UIApplicationShortcutItem?

    func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
        // Override point for customization after application launch.
        if let shortcutItem = launchOptions?[UIApplication.LaunchOptionsKey.shortcutItem] as? UIApplicationShortcutItem {
            shortcutItemToProcess = shortcutItem
            print("Shortcut pressed")
        }
        return true
    }

    func application(_ application: UIApplication, performActionFor shortcutItem: UIApplicationShortcutItem, completionHandler: (Bool) -> Void) {
        print("Shortcut pressed")
    }

    func applicationDidBecomeActive(_ application: UIApplication) {
        print("Shortcut pressed")
    }

    // MARK: UISceneSession Lifecycle

    func application(_ application: UIApplication, configurationForConnecting connectingSceneSession: UISceneSession, options: UIScene.ConnectionOptions) -> UISceneConfiguration {
        // Called when a new scene session is being created.
        // Use this method to select a configuration to create the new scene with.
        return UISceneConfiguration(name: "Default Configuration", sessionRole: connectingSceneSession.role)
    }

    func application(_ application: UIApplication, didDiscardSceneSessions sceneSessions: Set<UISceneSession>) {
        // Called when the user discards a scene session.
        // If any sessions were discarded while the application was not running, this will be called shortly after application:didFinishLaunchingWithOptions.
        // Use this method to release any resources that were specific to the discarded scenes, as they will not return.
    }


}

Thank you.

like image 546
Kurt Lane Avatar asked Oct 18 '19 20:10

Kurt Lane


1 Answers

Looks like your app is Scene based. For Scene based apps you can almost forget about the AppDelegate and focus on the SceneDelegate. There are two methods you now need to override in the SceneDelegate and one in the AppDelegate. I'll mimic Apple's guide for clarity:

If the user is opening the app and it's a fresh launch, you handle this in the AppDelegate:

     // AppDelegate.swift
     func application(_ application: UIApplication, configurationForConnecting connectingSceneSession: UISceneSession, options: UIScene.ConnectionOptions) -> UISceneConfiguration {
         // Called when a new scene session is being created.
         // Use this method to select a configuration to create the new scene with.

         // Grab a reference to the shortcutItem to use in the scene
         if let shortcutItem = options.shortcutItem {
             shortcutItemToProcess = shortcutItem
         }

         // Previously this method only contained the line below, where the scene is configured
         return UISceneConfiguration(name: "Default Configuration", sessionRole: connectingSceneSession.role)
     }

If your app is still running in the background when the user clicks on the shortcut item, you handle that in the SceneDelegate:

    // SceneDelegate.swift
    func windowScene(_ windowScene: UIWindowScene, performActionFor shortcutItem: UIApplicationShortcutItem, completionHandler: @escaping (Bool) -> Void) {
        // When the user opens the app through a quick action, this is now the method that will be called
        (UIApplication.shared.delegate as! AppDelegate).shortcutItemToProcess = shortcutItem
    }

Once the scene is ready, you can do what you need to with the shortcut :

    // SceneDelegate.swift
    func sceneDidBecomeActive(_ scene: UIScene) {
        // Is there a shortcut item that has not yet been processed?
        if let shortcutItem = (UIApplication.shared.delegate as! AppDelegate).shortcutItemToProcess {
            // In this sample an alert is being shown to indicate that the action has been triggered,
            // but in real code the functionality for the quick action would be triggered.
            var message = "\(shortcutItem.type) triggered"
            if let name = shortcutItem.userInfo?["Name"] {
                message += " for \(name)"
            }
            let alertController = UIAlertController(title: "Quick Action", message: message, preferredStyle: .alert)
            alertController.addAction(UIAlertAction(title: "Close", style: .default, handler: nil))
            window?.rootViewController?.present(alertController, animated: true, completion: nil)

            // Reset the shorcut item so it's never processed twice.
            (UIApplication.shared.delegate as! AppDelegate).shortcutItemToProcess = nil
        }
    }
like image 179
William Brawner Avatar answered Nov 15 '22 11:11

William Brawner