Sorry if this is a dumb question, Setting up a 3D Touch quick action. I put everything into the .plist file and I had to put this into my AppDelegate.swift file
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) }
and this into my SceneDelegate.swift
// Shortcut code
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
}
// Shortcut code
func sceneDidBecomeActive(_ scene: UIScene) {
// Is there a shortcut item that has not yet been processed?
if let shortcutItem = (UIApplication.shared.delegate as! AppDelegate).shortcutItemToProcess {
if shortcutItem.type == "com.application.Start" {
print("Start Shortcut pressed")
//let vc = ViewController ()
//vc.startAct()
}
// Reset the shorcut item so it's never processed twice.
(UIApplication.shared.delegate as! AppDelegate).shortcutItemToProcess = nil
}
}
I want to run the function startAct() { ... }
that's in the Main app file called ViewController.swift
I tried
let vc = ViewController ()
vc.startAct()
and it sort of starts to run but crashed straight away on the first line with an error about unwrapping a nil value or something. Im guessing its not actually loading the Main view but trying to run it from the SceneDelegate.swift which cannot be correct.
Thank you in advance.
configurationForConnecting
is for. I would just delete that method entirely if I were you; it serves no purpose in most apps. Your job is to implementwindowScene(_:performActionFor:completionHandler:)
and that's all; it will be called when the user taps an action. You are not implementing it correctly; you should not be interspersing this stuff aboutsceneDidBecomeActive
or storing the info in a global as you are doing. Just do whatever the action tells you to do, now. And call the completion handler! – Causerie