抱歉,如果这是一个愚蠢的问题,请设置3D Touch快速操作。我将所有内容都放入.plist文件中,并且不得不将其放入我的AppDelegate.swift文件中
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) }
并将其放入我的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
}
}
我想运行主应用程序文件ViewController.swift中的函数startAct() { ... }
我尝试过
let vc = ViewController ()
vc.startAct()
它开始运行,但在第一行立即崩溃,并释放了有关解开nil值或类似内容的错误。我猜测它实际上不是在加载Main视图,而是试图从SceneDelegate.swift运行它,这是不正确的。
谢谢你。
这似乎引起了一些混乱,所以我将展示我的工作。
您必须在两个位置实现对快捷方式项的响应:在场景委托的willConnectTo
和performActionFor
中。为了帮助您进行测试,我将删除所有错误检查和分析,并仅说明我们实际上在响应快捷方式项:
func scene(_ scene: UIScene, willConnectTo session: UISceneSession, options connectionOptions: UIScene.ConnectionOptions) {
if let shortcutItem = connectionOptions.shortcutItem {
let alert = UIAlertController(title: "Hello", message: "", preferredStyle: .alert)
alert.addAction(UIAlertAction(title: "OK", style: .default))
self.window?.makeKeyAndVisible()
self.window?.rootViewController?.present(alert, animated: true)
}
}
func windowScene(_ windowScene: UIWindowScene, performActionFor shortcutItem: UIApplicationShortcutItem, completionHandler: @escaping (Bool) -> Void) {
let alert = UIAlertController(title: "Hello", message: "", preferredStyle: .alert)
alert.addAction(UIAlertAction(title: "OK", style: .default))
self.window?.rootViewController?.present(alert, animated: true)
completionHandler(true)
}
这将显示警报,表明该应用是事先已暂停还是已终止。