29

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

代码:

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
            }
        }
    }
}

谢谢!

4

6 回答 6

62
  1. 在 Xcode 中,打开 Product -> Schemes -> Edit Schemes
  2. 在您的运行方案中,将启动设置更改为“等待启动可执行文件”

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

请参阅设置的 Xcode 中的屏幕截图

于 2015-10-12T21:55:01.987 回答
26

我终于得到了这个工作。这是我的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))

}

其中大部分来自 Apple 的UIApplicationShortcuts示例代码,当我让我的应用程序启动警报以证明它正在识别选择了正确的快捷方式时,这可以适应您的代码以弹出视图控制器。

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

于 2015-10-22T14:11:42.843 回答
3

对于斯威夫特 4.2

   func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
    // Override point for customization after application launch.
    var isLaunchedFromQuickAction = false
    if let shortcutItem = launchOptions?[UIApplication.LaunchOptionsKey.shortcutItem] as? UIApplicationShortcutItem {
        isLaunchedFromQuickAction = true
        handleQuickAction(shortcutItem: shortcutItem)
    }

    return isLaunchedFromQuickAction
}
于 2019-02-05T13:11:28.653 回答
1

我尝试了以上所有方法,但并没有解决问题,而是我尝试在 handleShortcut 方法延迟后处理快捷方式:

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

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

于 2015-12-22T16:59:31.037 回答
0

用这个替换你的 didfinishlaunching 方法。

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

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

    handleShortcut(shortcutItem)
    return false
  }
  return true
}
于 2018-02-05T11:26:43.453 回答
0

XCode 11.6,斯威夫特 5

我们可以在运行时附加一个进程。XCode 将等到进程启动,并在手动启动应用程序时附加到它。

XCode -> Debug -> Attach to process by PID or Name -> (" Enter the name of an app in the pop-up ")

方向:

  1. 确保应用程序新安装在设备或模拟器上。
  2. 杀死应用程序
  3. 在 XCode 中附加上面提到的进程名称。
  4. 通过所需的快捷方式打开应用程序

PS:如果您使用的是 SceneDelegate,可以在

func scene(_ scene: UIScene, willConnectTo session: UISceneSession, options connectionOptions: UIScene.ConnectionOptions) {
       switch connectionOptions.shortcutItem?.localizedTitle {
       case "Search":
       break
       case "DoSomething":
       break
       default:
       break
       } 
    }

快乐调试:)

调试 -> 进程名称

目标名称

于 2021-01-06T09:00:23.767 回答