使用 URL 打开应用程序时将数据从 Scene Delegate 传递到 ViewController

Passing data from Scene Delegate to ViewController when opening app with URL

我需要在我的初始控制器上显示一个视图,并在 Firebase 电子邮件验证期间通过 URL 动态 Link 启动应用程序时设置一个 UILabel。

我遇到的问题是标签未初始化,因此应用程序在此过程中崩溃。我尝试了很多方法,但无法在打开动态 Link 时显示我的视图并设置标签,即使应用程序已经打开。

场景代表

func scene(_ scene: UIScene, continue userActivity: NSUserActivity) {

    if let link = userActivity.webpageURL?.absoluteString
    {
     
        if Auth.auth().isSignIn(withEmailLink: link) {

            Config().verifyEmail(link: link)
        }
    }
}

AppDelegate

@main
class AppDelegate: UIResponder, UIApplicationDelegate {

var errorText: String?
var errorType: Bool?

func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
    
    FirebaseApp.configure()
    
    return true
}

ViewController

func loadErrorInfo()
{
    let appDelegate = UIApplication.shared.delegate as! AppDelegate

    if let eText = appDelegate.errorText, let eType = appDelegate.errorType {
        errorCall(message: eText, error: eType)
    } else {
        errorBox.isHidden = true
    }
}
func errorCall(message: String, error: Bool)
{
    if error {
        self.errorLabel.textColor = UIColor.yellow
        self.errorLabel.text = message
        self.errorBox.isHidden = false
        self.errorBox.shake()
    } else {
        self.errorLabel.textColor = UIColor.white
        self.errorLabel.text = message
        self.errorBox.isHidden = false
        self.errorBox.shakeL()
    }
}

自定义配置 NSObject Class

import UIKit
import FirebaseAuth

open class Config: NSObject {

public func verifyEmail(link: String)
{
    var email = ""; var password = ""
    if let x = UserDefaults.standard.object(forKey: "Email") as? String { email = x }
    if let y = UserDefaults.standard.object(forKey: "Password-\(email)") as? String { password = y }
    
    if password != "" && email != ""
    {
        Auth.auth().signIn(withEmail: email, link: link) { (user, error) in
        if let use = user, error == nil
        {
            Auth.auth().currentUser?.updatePassword(to: password) { (error) in
                  if let error = error
                  {
                        print(error)
                    let appDelegate = UIApplication.shared.delegate as! AppDelegate
                    appDelegate.errorText = error.localizedDescription
                        appDelegate.errorType = true
                    ViewController().loadErrorInfo()
                  }
                  else
                  {
                    print(use, "Logged In")
                    let appDelegate = UIApplication.shared.delegate as! AppDelegate
                    appDelegate.errorText = "\(use) Logged In"
                    appDelegate.errorType = false
                    ViewController().loadErrorInfo()
       
                  }
               }
            }
        }
    }
}

}

不确定还有什么其他方法可行。总而言之,在单击电子邮件中的动态 Link 后,我想在主 ViewController 上更改我的 UILabel,这是一个初始控制器。目前,由于未设置 UILabel,每种方法都会导致它崩溃,即使控制器已经初始化也是如此。

设法通过再次初始化控制器并更新根控制器来修复它。

// 场景代理

func changeRootViewController(_ vc: UIViewController, animated: Bool = true)
{
    guard let window = self.window else {
        return
    }
    window.rootViewController = vc
}

//重新初始化

let storyboard = UIStoryboard(name: "Main", bundle: nil)
let destinationNavigationController = storyboard.instantiateViewController(withIdentifier: "ViewController2")
(UIApplication.shared.connectedScenes.first?.delegate as? SceneDelegate)?.changeRootViewController(destinationNavigationController)