Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ios/115.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios 如何从onContinueUserActivity更改WindowGroup视图?_Ios_Swiftui_Ios14 - Fatal编程技术网

Ios 如何从onContinueUserActivity更改WindowGroup视图?

Ios 如何从onContinueUserActivity更改WindowGroup视图?,ios,swiftui,ios14,Ios,Swiftui,Ios14,我有一个深度链接的应用程序,所以首先我要呈现一个加载或主视图: @main struct AppMain: App { var body: some Scene { WindowGroup { LoadingView() .onContinueUserActivity(NSUserActivityTypeBrowsingWeb, perform: handleUserActivity) }

我有一个深度链接的应用程序,所以首先我要呈现一个加载或主视图:

@main
struct AppMain: App {
    
    var body: some Scene {
        WindowGroup {
            LoadingView()
                .onContinueUserActivity(NSUserActivityTypeBrowsingWeb, perform: handleUserActivity)
        }
    }
}
但是,在响应活动的my
handleUserActivity
中,如何根据活动参数更新屏幕?例如,我提取了一个值,并希望从这里渲染视图:

private extension AppMain {
    
    func handleUserActivity(_ userActivity: NSUserActivity) {
        guard let url = userActivity.webpageURL,
              let components = NSURLComponents(url: url, resolvingAgainstBaseURL: true),
              let queryItems = components.queryItems
        else {
            return
        }
        
        if let modelID = queryItems.first(where: { $0.name == "id" })?.value {
            SmoothieView(id: modelID) // TODO: How to I render view??
        }
    }
}

如何替换加载视图并显示平滑视图?

这里有一种可能的方法-使用顶部视图中的应用程序状态对象和观察者根据状态进行切换

(注意:代码是在适当的位置键入的,因此可能会出现拼写错误)

class AppState:ObservableObject{
@已发布的var modelID:String?
}
@主要
结构AppMain:App{
@StateObject变量appState=appState()
var body:一些场景{
窗口组{
ContentView()
.environmentObject(appState)
.onContinueUserActivity(NSUserActivityTypeBrowsingWeb,执行:handleUserActivity)
}
}
}
专用扩展AppMain{
func handleUserActivity(uUserActivity:NSUserActivity){
guard let url=userActivity.webpageURL,
让components=NSURLComponents(url:url,resolvingAgainstBaseURL:true),
让queryItems=components.queryItems
否则{
返回
}
如果让modelID=queryItems.first(其中:{$0.name==“id”})?.value{
self.appState.modelID=modelID//
class AppState: ObservableObject {
   @Published var modelID: String?
}

@main
struct AppMain: App {
    @StateObject var appState = AppState()

    var body: some Scene {
        WindowGroup {
            ContentView()
                .environmentObject(appState)
                .onContinueUserActivity(NSUserActivityTypeBrowsingWeb, perform: handleUserActivity)
        }
    }
}

private extension AppMain {
    
    func handleUserActivity(_ userActivity: NSUserActivity) {
        guard let url = userActivity.webpageURL,
              let components = NSURLComponents(url: url, resolvingAgainstBaseURL: true),
              let queryItems = components.queryItems
        else {
            return
        }
        
        if let modelID = queryItems.first(where: { $0.name == "id" })?.value {

            self.appState.modelID = modelID // << here !!

        }
    }
}

// as soon as you receive model id the view will be refreshed
struct ContentView: View {
   @EnvironmentObject var appState: AppState

   var body: some View {
      if appState.modelID == nil {
         LoadingView()
      } else {
         SmoothieView(id: appState.modelID!)
      }
   }
}