根据Apple文档:
您有责任确保系统有条件地调用此方法,具体取决于您是否启动了一个应用
方法(应用程序:willFinishLaunchingWithOptions:或
application:didFinishLaunchingWithOptions :)已经处理了
快速动作调用。系统调用启动方法(之前
调用此方法),当用户为您的应用选择快速操作时
然后您的应用会启动而不是启动。要求的快速
动作可能使用与其他方式不同的代码路径
应用启动时。例如,假设您的应用正常启动到
显示视图A,但您的应用是为快速响应而启动的
需要视图B的操作。要处理此类情况,请在启动时进行检查,
您的应用是否通过快速操作启动。执行这个
签入您的应用程序:willFinishLaunchingWithOptions:或
application:didFinishLaunchingWithOptions:方法,方法是检查
UIApplicationLaunchOptionsShortcutItemKey启动选项键。的
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。
关于ios - didFinishLaunching中的UIApplicationShortcutItem,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/35083367/