簡體   English   中英

didFinishLaunching中的UIApplicationShortcutItem

[英]UIApplicationShortcutItem in didFinishLaunching

根據Apple文檔:

您有責任確保系統有條件地調用此方法,具體取決於您的應用啟動方法之一(application:willFinishLaunchingWithOptions:或application:didFinishLaunchingWithOptions :)是否已經處理了快速操作調用。 當用戶為您的應用選擇快速操作並且應用啟動而不是激活時,系統將調用啟動方法(在調用此方法之前)。 請求的快速操作所采用的代碼路徑可能與應用啟動時使用的代碼路徑不同。 例如,假設您的應用程序通常啟動以顯示視圖A,但是您的應用程序是為響應需要視圖B的快速操作而啟動的。要處理此類情況,請在啟動時檢查是否通過快速操作啟動了您的應用程序。 通過檢查UIApplicationLaunchOptionsShortcutItemKey啟動選項鍵,在application:willFinishLaunchingWithOptions:或application:didFinishLaunchingWithOptions:方法中執行此檢查。 UIApplicationShortcutItem對象可用作啟動選項鍵的值。

但是,為什么需要在didfinishlauncing / willfinishLauncing方法中處理此問題。 如果應用程序處於終止狀態,則最終它將調用performActionForShortcutItem方法,那么為什么需要簽入didfinish方法?

樣例代碼:

//code




    func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
        var launchedFromShortCut = false
        //Check for ShortCutItem
        if let shortcutItem = launchOptions?[UIApplicationLaunchOptionsShortcutItemKey] as? UIApplicationShortcutItem {
            launchedFromShortCut = true
            handleShortCutItem(shortcutItem)
        }

        //Return false incase application was lanched from shorcut to prevent
        //application(_:performActionForShortcutItem:completionHandler:) from being called
        return !launchedFromShortCut
    }

    func application(application: UIApplication, performActionForShortcutItem shortcutItem: UIApplicationShortcutItem, completionHandler: Bool -> Void) {
        let handledShortCutItem = handleShortCutItem(shortcutItem)
        completionHandler(handledShortCutItem)
    }

    func handleShortCutItem(shortcutItem: UIApplicationShortcutItem) -> Bool {
        var handled = false
        //Get type string from shortcutItem
        if let shortcutType = ShortcutType.init(rawValue: shortcutItem.type) {
            //Get root navigation viewcontroller and its first controller
            let rootNavigationViewController = window!.rootViewController as? UINavigationController
            let rootViewController = rootNavigationViewController?.viewControllers.first as UIViewController?
            //Pop to root view controller so that approperiete segue can be performed
            rootNavigationViewController?.popToRootViewControllerAnimated(false)

            switch shortcutType {
            case .Torquiose:
                rootViewController?.performSegueWithIdentifier(toTurquoiseSeque, sender: nil)
                handled = true
            case.Red:
                rootViewController?.performSegueWithIdentifier(toRedSeque, sender: nil)
                handled = true
            }
        }
        return handled
    }
}

它使您可以靈活地決定-您可以在didFinishLaunching中“盡早”處理它-返回FALSE將阻止稍后調用performActionForShortcutItem。 或者您可以在didFinishLaunching中返回TRUE,並且仍然會調用performActionForShortcutItem。

暫無
暫無

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

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