检测 Launch 的源应用程序已弃用?替代品?

问题描述

我试图检测是否有其他应用程序启动了我的应用程序,以便启动不同的视图来接受传递给它的 pdf。看起来 LaunchOptionsKey 可以在 AppDelegate 中使用,但以下代码似乎不起作用。谁能告诉我是我使用的代码错误还是有其他选择。大多数参考文献都使用 sourceApplication 来做到这一点,但我读到了 2019 年已弃用的声明,但苹果的文档中仍然有它吗?我还注意到切换启动视图仅适用于 SceneDelegate 而不是 AppDelegate。显然这也是最近的变化?

class AppDelegate: UIResponder,UIApplicationDelegate {

func application(_ application: UIApplication,didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
  print("finished launching")
    
    if launchOptions?[UIApplication.LaunchOptionsKey.sourceApplication] != nil {
        print("Launched by another app")
        startVC = "addFile"
    } else {
        print("source app not identified")
    }

    return true
}

class SceneDelegate: UIResponder,UIWindowSceneDelegate {

var window: UIWindow?

func scene(_ scene: UIScene,willConnectTo session: UIScenesession,options connectionoptions: UIScene.Connectionoptions) {
    
    print("Scene: finished launching")
      //self.window = UIWindow(frame: UIScreen.main.bounds) // black sreen
    let storyBoard = UIStoryboard.init(name: "Main",bundle: nil)
    if startVC == "addFile" {
        let initialVc = storyBoard.instantiateViewController(withIdentifier:  "addFile") as! addFileViewController
        self.window?.rootViewController = initialVc
        self.window?.makeKeyAndVisible()       
    }
    if startVC == "home" {
        let initialVc = storyBoard.instantiateViewController(withIdentifier:  "home") as! FirstController
        self.window?.rootViewController = initialVc
        self.window?.makeKeyAndVisible()
    }
   guard let _ = (scene as? UIWindowScene) else { return }
}

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)