gpt4 book ai didi

ios - didFinishLaunching 中的 UIApplicationShortcutItem

转载 作者:搜寻专家 更新时间:2023-11-01 05:37:31 26 4
gpt4 key购买 nike

根据 Apple 文档:

It’s your responsibility to ensure the system calls this method conditionally, depending on whether or not one of your app launch methods (application:willFinishLaunchingWithOptions: or application:didFinishLaunchingWithOptions:) has already handled a quick action invocation. The system calls a launch method (before calling this method) when a user selects a quick action for your app and your app launches instead of activating. The requested quick action might employ code paths different than those used otherwise when your app launches. For example, say your app normally launches to display view A, but your app was launched in response to a quick action that needs view B. To handle such cases, check, on launch, whether your app is being launched via a quick action. Perform this check in your application:willFinishLaunchingWithOptions: or application:didFinishLaunchingWithOptions: method by checking for the UIApplicationLaunchOptionsShortcutItemKey launch option key. The UIApplicationShortcutItem object is available as the value of the launch option key.

但是为什么需要在 didfinishlauncing/willfinishLauncing 方法中处理这个。如果应用处于kill状态,最终它会调用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
}
}

最佳答案

它为您提供了决定的灵 active - 您可以在 didFinishLaunching 中“尽早”处理它 - 返回 FALSE 将禁止稍后调用 performActionForShortcutItem。或者您可以在 didFinishLaunching 中返回 TRUE,并且 performActionForShortcutItem 仍将被调用。

关于ios - didFinishLaunching 中的 UIApplicationShortcutItem,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35083367/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com