2015-09-26 105 views
14

我正在为我的iOS 9应用在swift中实施一些3D触摸快速操作,并且我有一个奇怪的问题。当我的应用程序处于后台并且我使用快速操作启动时,一切都按计划进行。当我的应用程序完全死亡(即我从多任务菜单中杀死它),并且我以快速操作启动时,应用程序崩溃。我在调试时遇到了麻烦,因为一旦我终止了应用程序,Xcode中的调试会话就会被分离。有没有办法让我连接到应用程序进行调试像正常,或者是否有我的代码中会导致它的东西?提前致谢。使用UIApplicationShortcutItem启动

代码:

func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool 
{ 
    var launchedFromShortCut = false 

    //Check for ShortCutItem 
    if let shortcutItem = launchOptions?[UIApplicationLaunchOptionsShortcutItemKey] as? UIApplicationShortcutItem 
    { 
     launchedFromShortCut = true 
     self.handleShortCutItem(shortcutItem) 
    } 

    return !launchedFromShortCut 
} 

func application(application: UIApplication, performActionForShortcutItem shortcutItem: UIApplicationShortcutItem, completionHandler: (Bool) -> Void) 
{ 
    self.handleShortCutItem(shortcutItem) 
} 

func handleShortCutItem(shortcutItem: UIApplicationShortcutItem) 
{ 
    //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 


     if let rootViewController = rootNavigationViewController?.viewControllers.first as! LaunchViewController? 
     { 
      //Pop to root view controller so that approperiete segue can be performed 
      rootNavigationViewController?.popToRootViewControllerAnimated(false) 

      switch shortcutType 
      { 
       case .Compose: 
        rootViewController.shouldCompose() 
        break 
      } 
     } 
    } 
} 

谢谢!

+0

你可以在死后看应用程序的崩溃日志文件... –

+0

你得到这个排序,我有同样的问题? – theiOSDude

+0

@theiOSDude不,我没有。 –

回答

15

我终于得到了这个工作。这是我的AppDelegate.swift文件结束为;

class AppDelegate: UIResponder, UIApplicationDelegate { 

// Properties 
var window: UIWindow? 
var launchedShortcutItem: UIApplicationShortcutItem? 

func applicationDidBecomeActive(application: UIApplication) { 

    guard let shortcut = launchedShortcutItem else { return } 

    handleShortcut(shortcut) 

    launchedShortcutItem = nil 

} 

func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool { 

    // Override point for customization after application launch. 
    var shouldPerformAdditionalDelegateHandling = true 

    // If a shortcut was launched, display its information and take the appropriate action 
    if let shortcutItem = launchOptions?[UIApplicationLaunchOptionsShortcutItemKey] as? UIApplicationShortcutItem { 

     launchedShortcutItem = shortcutItem 

     // This will block "performActionForShortcutItem:completionHandler" from being called. 
     shouldPerformAdditionalDelegateHandling = false 

    } 

    return shouldPerformAdditionalDelegateHandling 
} 


func handleShortcut(shortcutItem:UIApplicationShortcutItem) -> Bool { 

    // Construct an alert using the details of the shortcut used to open the application. 
    let alertController = UIAlertController(title: "Shortcut Handled", message: "\"\(shortcutItem.localizedTitle)\"", preferredStyle: .Alert) 
    let okAction = UIAlertAction(title: "OK", style: .Default, handler: nil) 
    alertController.addAction(okAction) 

    // Display an alert indicating the shortcut selected from the home screen. 
    window!.rootViewController?.presentViewController(alertController, animated: true, completion: nil) 

    return handled 

} 

func application(application: UIApplication, performActionForShortcutItem shortcutItem: UIApplicationShortcutItem, completionHandler: (Bool) -> Void) { 

    completionHandler(handleShortcut(shortcutItem)) 

} 

这在很大程度上是由苹果公司sample code为UIApplicationShortcuts拍摄,虽然我有我的应用程序启动警报,证明它是认识选择适当的快捷方式,这样可以适应您的代码以弹出视图控制器。

我认为func applicationDidBecomeActive是我失踪的关键部分,并且从didFinishLaunchingWithOptions中删除了self.handleShortCut(shortcutItem)(否则它似乎是两次调用handleShortCut)。

+0

这真棒很快就会测试。 –

+0

你回报的“已处理”价值是什么? –

37
  1. 在Xcode中,开放式产品 - >方案 - >编辑方案
  2. 运行中的计划,改启动设置为“等待执行上马”现在

,如果打开在调试并运行您的应用程序时,Xcode将等待您从主屏幕启动您的应用程序,以便您可以使用3D Touch Shortcut Item启动它。

See Screenshot in Xcode of the Setting

+1

非常感谢!这使我能够从终止状态测试快速行动。 +1 –

+1

这不会打印控制台中的日志? – genaks

+2

如何将内容打印到控制台? – iOShepherd

2

我尝试了所有的上面,而且比我试图在处理方法handleShortcut延迟后的快捷方式并没有解决问题 :

self.performSelector("action1", withObject: self, afterDelay: 0.5) 

,并添加一个方法的每一个动作,它的工作就像魅力

0

用这个替换你的didfinishlaunching方法。

func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject : AnyObject]?) -> Bool { 

    if let shortcutItem = 
     launchOptions?[UIApplicationLaunchOptionsShortcutItemKey] 
     as? UIApplicationShortcutItem { 

    handleShortcut(shortcutItem) 
    return false 
    } 
    return true 
}