【问题标题】:3D Touch quick actions not working when app first launches应用程序首次启动时 3D Touch 快速操作不起作用
【发布时间】:2016-04-30 00:40:10
【问题描述】:

我在使用 3D Touch 时遇到了一些问题。它运行良好,除非应用程序首次启动。这是我的 AppDelegate 中的以下代码:

 func handleQuickAction(shortcutItem: UIApplicationShortcutItem) -> Bool {
    var quickActionHandled = false
    let type = shortcutItem.type.componentsSeparatedByString(".").last!
    if let shortcutType = Shortcut(rawValue: type) {
        switch shortcutType {
        case .aboutMe:
            NSNotificationCenter.defaultCenter().addObserver(RAIntroductionViewController(), selector: #selector(RAIntroductionViewController.aboutMeTap), name: "about", object: nil)
            NSNotificationCenter.defaultCenter().postNotificationName("about", object: self)
            quickActionHandled = true
        case .education:
            NSNotificationCenter.defaultCenter().addObserver(RAIntroductionViewController(), selector: #selector(RAIntroductionViewController.educationTap), name: "education", object: nil)
            NSNotificationCenter.defaultCenter().postNotificationName("education", object: self)
            quickActionHandled = true
        case .projects:
            NSNotificationCenter.defaultCenter().addObserver(RAIntroductionViewController(), selector: #selector(RAIntroductionViewController.projectsTap), name: "projects", object: nil)
            NSNotificationCenter.defaultCenter().postNotificationName("projects", object: self)
            quickActionHandled = true
        case .why:
            NSNotificationCenter.defaultCenter().addObserver(RAIntroductionViewController(), selector: #selector(RAIntroductionViewController.whyPressed(_:)), name: "why", object: nil)
            NSNotificationCenter.defaultCenter().postNotificationName("why", object: self)
            quickActionHandled = true
        }
    }
    return quickActionHandled
}

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

}

这是RAIntroductionViewControllerviewDidLoad方法中的代码:

 NSNotificationCenter.defaultCenter().addObserver(self, selector: #selector(self.aboutMeTap), name: "about", object: nil)
    NSNotificationCenter.defaultCenter().addObserver(self, selector: #selector(self.educationTap), name: "education", object: nil)
    NSNotificationCenter.defaultCenter().addObserver(self, selector: #selector(self.projectsTap), name: "projects", object: nil)
    NSNotificationCenter.defaultCenter().addObserver(self, selector: #selector(self.whyPressed(_:)), name: "why", object: nil)

那些观察者调用推送导航控制器的方法。

虽然一切正常,但导航控制器在应用首次启动时并未推送到正确的页面。它只是转到根视图控制器。

【问题讨论】:

    标签: ios swift 3dtouch


    【解决方案1】:

    当应用程序首次启动时,application:performActionForShortcutItem shortcutItem:completionHandler: 不会被调用。

    相反,您应该在 application:didFinishLaunchingWithOptions: 中处理此问题。

    func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {        
        if let options = launchOptions,
           let shortcutItem = options[UIApplicationLaunchOptionsShortcutItemKey] as? UIApplicationShortcutItem {
           // do the work here..
        }
        return true
    }
    

    希望这会有所帮助:)

    【讨论】:

    • 感谢您的回复。我试过了,但我得到一个错误:在展开可选值时意外发现 nil。我假设shortcutItem 变量为零。有什么想法吗?
    • 很高兴听到您的反馈,该代码仅在 launchOptions 不是 nil 并且有 UIApplicationLaunchOptionsShortcutItemKey 的值时才有效。我已经更新了代码,所以if 语句的主体只有在应用程序从 3D Touch 快捷方式启动时才会执行。您介意尝试一下新代码,看看它是否有效吗?谢谢!
    • 感谢您的更新。我尝试了这段代码,但什么也没发生。没有错误或崩溃,但它没有转到正确的页面。澄清一下,我正在使用 NSNotificationCenter 添加观察者来执行此操作。
    • 我在if语句中插入了一个print语句,竟然没有执行。
    • 您介意我们在聊天中讨论这个问题并一起找出解决方案吗? chat.stackoverflow.com/rooms/110695/…
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-07-23
    • 2012-08-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-12-27
    相关资源
    最近更新 更多