OGeek|极客世界-中国程序员成长平台

标题: ios - 从本地通知启动应用程序; VC实例化,但没有出现 [打印本页]

作者: 菜鸟教程小白    时间: 2022-12-11 19:42
标题: ios - 从本地通知启动应用程序; VC实例化,但没有出现

我想从本地通知启动我的应用程序,当主屏幕被锁定或用户在另一个应用程序中时,该通知将基于类似的讨论出现herehere我的 AppDelegate 中有以下代码:

func userNotificationCenter(_: UNUserNotificationCenter, didReceive response: UNNotificationResponse, withCompletionHandler completionHandler: @escaping () -> Void) {

        switch response.actionIdentifier {

        case "play":
                var setAlarmVC = self.window?.rootViewController as? SettingAlarmViewController
                if setAlarmVC == nil {
                    let storyboard = UIStoryboard(name: "Main", bundle: nil)
                    setAlarmVC = storyboard.instantiateViewController(withIdentifier: "AlarmViewController") as? SettingAlarmViewController
                    self.window = UIWindow(frame: UIScreen.main.bounds)
                    self.window?.rootViewController = setAlarmVC
                    self.window?.makeKeyAndVisible()
                }

        case "snooze":
            print("I pressed pause")
        default:
            break
        }

        completionHandler()

    }

在我的 SettingAlarmViewController 的 viewDidLoad 中,我设置了一些简单的打印输出

override func viewDidLoad() {
    super.viewDidLoad()
    print("Setting Alarm View Controller was instantiated")
}

当我在应用程序处于后台时按本地通知中的播放时,我会按预期打印控制台:

Setting Alarm View Controller was instantiated

但应用程序并没有真正启动并且设置警报 View Controller 没有出现。如果我然后单击该应用程序,一个新的设置警报 View Controller 是第一个可见的东西。我觉得我一定错过了一些明显的东西,但我无法弄清楚它是什么。

编辑:做更多的测试。当通知出现在锁定屏幕上并且用户从锁定屏幕按“播放”时,密码/解锁屏幕不会出现,但应用程序仍然启动并且我得到打印输出“设置警报 View Controller 已实例化”



Best Answer-推荐答案


好吧,我浪费了一天的时间,这是问题所在,因此其他人不会遇到同样的问题:developer.apple.com/documentation/usernotifications/...您必须在通知操作中添加 [.foreground] , 如 let playAction = UNNotificationAction(identifier: "play", title: "play", options: [.foreground])

关于ios - 从本地通知启动应用程序; VC实例化,但没有出现,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48610469/






欢迎光临 OGeek|极客世界-中国程序员成长平台 (http://ogeek.cn/) Powered by Discuz! X3.4