Swift Scene Delegate 在首次启动时不 运行 代码

Swift Scene Delegate doesn't run code on first launch

我有一段代码在使用 UTI 导入文件打开应用程序时在 Scene Delegate 中运行。代码工作正常,但应用程序必须 运行 并加载视图控制器才能执行,我该怎么做,当应用程序通过单击文件启动时,代码也是 运行?

SceneDelegate.swift

class SceneDelegate: UIResponder, UIWindowSceneDelegate {

var window: UIWindow?


func scene(_ scene: UIScene, willConnectTo session: UISceneSession, options connectionOptions: UIScene.ConnectionOptions) {

    guard let _ = (scene as? UIWindowScene) else { return }
}
func scene(_ scene: UIScene, openURLContexts URLContexts: Set<UIOpenURLContext>) {
      if let url = URLContexts.first?.url {
          // Handle URL
          let needTo = url.startAccessingSecurityScopedResource()

          do {
            let data = try Data(contentsOf: url)
            let contents = String(
              data: data,
              encoding: String.Encoding.utf8
            )
              if let x = contents
              {

                  MatchDecoder.decodeText(content: x)
                  
              }
          } catch(let error) { print(error) }

          if needTo {
            url.stopAccessingSecurityScopedResource()
          }
      }
  }
//....

设法修复它,对于遇到相同问题的任何人,请添加此内容,现在 URL 将在首次启动时也是 运行!

func scene(_ scene: UIScene, willConnectTo session: UISceneSession, options connectionOptions: UIScene.ConnectionOptions) {
    
    guard let _ = (scene as? UIWindowScene) else { return }
    self.scene(scene, openURLContexts: connectionOptions.urlContexts)
}