相关疑难解决方法(0)

我想点击推送通知以在 SwiftUI 中打开特定屏幕

我正在使用 SwiftUI。

我想通过单击推送通知来打开 Root View 以外的特定屏幕。有几种方法可以使用 StoryBoard 打开它,但不能没有 StoryBoard。

如何在不使用 StoryBoard 的情况下实现它?

我试过这个,但我是初学者,所以我不知道。

class AppDelegate: UIResponder, UIApplicationDelegate, UNUserNotificationCenterDelegate {
    var window: UIWindow?

    func userNotificationCenter(
        _ center: UNUserNotificationCenter,
        willPresent notification: UNNotification,
        withCompletionHandler completionHandler: @escaping (UNNotificationPresentationOptions)
        -> Void) {
        completionHandler([.alert, .badge, .sound])
    }

    func userNotificationCenter(_ center: UNUserNotificationCenter, didReceive response: UNNotificationResponse, withCompletionHandler completionHandler: () -> Void) {
        // I want to open a screen other than Root View here.
        completionHandler()
    }
    ... }
Run Code Online (Sandbox Code Playgroud)

push-notification apple-push-notifications swift swiftui

9
推荐指数
1
解决办法
1000
查看次数

App Delegate 访问环境对象

我在一个类中有一个变量(一个描述游戏的标签),我需要在我的视图之间传递它,我通过 @EnvironmentObject 状态来实现。当一个更改该标签的函数(与变量在同一类中)被一个视图调用时,该变量会在其他视图中更新。但是,当通知被触发时,该函数也会被 AppDelegate 调用。目前,我已经将包含标签的类声明为 AppDelegate 中的新实例,这导致视图/结构中的变量没有更改。

是否可以让 AppDeleagte 访问环境对象(例如,通过 AppDelegate().environmentobject(myClass),如果是这样,在哪里?)还是有更好的方法来做到这一点?

简化代码:

包含播放列表标签和更改播放列表和标签的函数的类

class MusicManager: NSObject, ObservableObject {

    var playlistLabel: String = ""

    func playPlaylistNow(chosenPlaylist: String?) {  
        playlistLabel = "Playlist: \(chosenPlaylist!)"
    }

}
Run Code Online (Sandbox Code Playgroud)

显示标签的主页视图

struct HomeView: View {

    @EnvironmentObject var musicManager: MusicManager

    var body: some View {

        Text(musicManager.playlistLabel)

    }

}
Run Code Online (Sandbox Code Playgroud)

应用程序委托

class AppDelegate: UIResponder, UIApplicationDelegate, AVAudioPlayerDelegate {

    var musicManager: MusicManager = MusicManager()

        func application(_ application: UIApplication, didReceive notification: UILocalNotification) {
            var playlistName: String = ""
            if let userInfo = notification.userInfo …
Run Code Online (Sandbox Code Playgroud)

appdelegate swift swiftui

4
推荐指数
1
解决办法
3495
查看次数