簡體   English   中英

快捷方式,快速操作 3D Touch swift

[英]Shortcut Items, Quick Actions 3D Touch swift

由於某種原因,我在找出我的應用程序中的快捷方式項目時遇到了一些麻煩。 我遵循了所有的教程,我相信我做的一切都是正確的; 然而,它的表現真的很奇怪。 問題在於,當您完全關閉該應用程序並在主屏幕上執行 3D Touch Shortcut 項目時,它會將您帶到該應用程序的正確部分; 但是,當應用程序在后台打開時(就像您只需單擊主頁按鈕),它不會將您帶到應用程序的正確部分,它只會打開它。 這真的很奇怪,我不知道該怎么做,因為我遵循了所有說明。 太感謝了!

代碼:

func application(_ application: UIApplication, performActionFor shortcutItem: UIApplicationShortcutItem, completionHandler: @escaping (Bool) -> Void) {

    if shortcutItem.type == "com.myapp.newMessage" {

        let sb = UIStoryboard(name: "Main", bundle: nil)


        let vc = sb.instantiateViewController(withIdentifier: "RecentVC") as! UITabBarController
         vc.selectedIndex = 2 //this takes me to the contacts controller



        let root = UIApplication.shared.keyWindow?.rootViewController

        root?.present(vc, animated: false, completion: {
            completionHandler(true)
        })


    } else if shortcutItem.type == "com.myapp.groups" {
        let sb = UIStoryboard(name: "Main", bundle: nil)


        let vc = sb.instantiateViewController(withIdentifier: "RecentVC") as! UITabBarController
        vc.selectedIndex = 1 //this takes me to the groups controller

        let root = UIApplication.shared.keyWindow?.rootViewController

        root?.present(vc, animated: false, completion: {
            completionHandler(true)
        })

    } else {
        //more short cut items I will add later
    }
}

注意:這些是靜態快捷方式項,我在 info.plist 文件中配置了它們,顯示的這個功能是應用程序中除了 info.plist 文件中包含有關快捷方式項的任何內容的唯一地方。

編輯:這似乎是 RootViewController 的問題,因為我嘗試了多種不同的方法,但它仍然給我相同的警告消息

2017-11-22 22:36:46.473195-0800 QuickChat2.0[3055:1068642] 警告:嘗試呈現不在窗口層次結構中的視圖!

我只是試圖通過這篇文章以這種方式實現我的目標,但它給了我與以前完全相同的結果。

您應該在performActionForShortcutItem中設置一個變量,告訴您快捷方式類型並將您在其中的代碼移動到applicationDidBecomeActive 你的可能看起來像這樣

var shortcut: String?

func applicationDidBecomeActive(application: UIApplication) {
    if let shortcutItem = shortcut {
        shortcut = nil
        if shortcutItem == "com.myapp.newMessage" {
            let sb = UIStoryboard(name: "Main", bundle: nil)
            let vc = sb.instantiateViewController(withIdentifier: "RecentVC") as! UITabBarController
            vc.selectedIndex = 2 //this takes me to the contacts controller
            let root = UIApplication.shared.keyWindow?.rootViewController
            root?.present(vc, animated: false, completion: nil)
        } else if shortcutItem == "com.myapp.groups" {
            let sb = UIStoryboard(name: "Main", bundle: nil)
            let vc = sb.instantiateViewController(withIdentifier: "RecentVC") as! UITabBarController
            vc.selectedIndex = 1 //this takes me to the groups controller
            let root = UIApplication.shared.keyWindow?.rootViewController
            root?.present(vc, animated: false, completion: nil)
        } else {
            //more short cut items I will add later
        }
    }
}

func application(_ application: UIApplication, performActionFor shortcutItem: UIApplicationShortcutItem, completionHandler: @escaping (Bool) -> Void) {
    shortcut = shortcutItem.type
    completionHandler(true)
}

我認為更簡潔的方法是在 performActionFor 方法中設置標志,然后在 VC 加載后檢查它們。 每次應用程序變為活動狀態時,包括啟動,使用此通知都會處理快捷方式。

override func viewDidLoad() {
    super.viewDidLoad()
    // Sends notification if active app is reentered to handle quick actions
    NotificationCenter.default.addObserver(self, selector: #selector(handleQuickActions), name: UIApplication.didBecomeActiveNotification, object: nil)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM