1

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

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

场景代表

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

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

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

应用委托

@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
}

视图控制器

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 类

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()
       
                  }
               }
            }
        }
    }
}

}

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

4

1 回答 1

0

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

// 场景委托

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)
于 2020-11-02T11:19:31.967 回答