使用UIApplicationShortcutItem启动

Con*_*cks 26 ios swift ios9 3dtouch

我正在为我的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
            }
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

谢谢!

And*_*vel 49

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

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

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

  • 如何将内容打印到控制台? (2认同)

Zba*_*itZ 23

我终于搞定了这个.这是我的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))

}
Run Code Online (Sandbox Code Playgroud)

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

我觉得func applicationDidBecomeActive是,我是失踪,取出的重要组成部分self.handleShortCut(shortcutItem),从didFinishLaunchingWithOptions(否则它调用handleShortCut两次,它似乎).