Ios UIApplicationShortcutItem在didFinishLaunching中的应用
根据苹果的文档: 您有责任确保系统有条件地调用此方法,具体取决于您的某个应用程序是否启动 方法(应用程序:willFinishLaunchingWithOptions:或 应用程序:didFinishLaunchingWithOptions:)已处理 快速动作调用。系统调用一个启动方法(在 当用户选择应用程序的快速操作时调用此方法) 您的应用程序将启动而不是激活。请求快速响应 操作可能使用与其他操作不同的代码路径 当你的应用程序启动时。例如,假设您的应用程序正常启动到 显示视图A,但您的应用程序是为响应快速响应而启动的 需要视图B的操作。要处理此类情况,请在启动时进行检查, 是否通过快速操作启动您的应用程序。执行此操作 签入应用程序:willFinishLaunchingWithOptions:或 application:didFinishLaunchingWithOptions:method通过检查 UIApplicationLaunchProctionsShortcutItemKey启动选项键。这个 UIApplicationShortcutItem对象作为 启动选项键 但是为什么需要在didfinishlauncing/willfinishLauncing方法中处理这个问题呢。如果应用程序处于终止状态,它最终将调用performationforshortcutitem方法,那么为什么需要签入didfish方法呢 示例代码:Ios UIApplicationShortcutItem在didFinishLaunching中的应用,ios,swift,3dtouch,Ios,Swift,3dtouch,根据苹果的文档: 您有责任确保系统有条件地调用此方法,具体取决于您的某个应用程序是否启动 方法(应用程序:willFinishLaunchingWithOptions:或 应用程序:didFinishLaunchingWithOptions:)已处理 快速动作调用。系统调用一个启动方法(在 当用户选择应用程序的快速操作时调用此方法) 您的应用程序将启动而不是激活。请求快速响应 操作可能使用与其他操作不同的代码路径 当你的应用程序启动时。例如,假设您的应用程序正常启动到 显示视图A,但您的应用程序
//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将阻止PerformationForShortcutItem稍后被调用。或者,您可以在didFinishLaunching中返回TRUE,并且仍然会调用PerformationForShortcutItem