根据Apple文档:
您有责任确保系统有条件地调用此方法,具体取决于您的某个应用是否启动 方法(应用程序:willFinishLaunchingWithOptions:或 应用程序:didFinishLaunchingWithOptions :)已经处理了一个 快速动作调用。系统调用启动方法(之前 当用户为您的应用选择快速操作时,调用此方法) 并启动您的应用程序而不是激活。要求快 action可能会使用与其他方式不同的代码路径 当你的应用程序启动时。例如,假设您的应用通常会启动 显示视图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
}
}
答案 0 :(得分:0)
它使您可以灵活地决定 - 您可以在didFinishLaunching中“早期”处理它 - 返回FALSE将禁止performActionForShortcutItem稍后调用。或者你可以在didFinishLaunching中返回TRUE,并且仍然会调用performActionForShortcutItem。