【问题标题】:Passing data from Scene Delegate to ViewController when opening app with URL使用 URL 打开应用程序时将数据从场景委托传递到 ViewController
【发布时间】:2021-02-06 14:36:05
【问题描述】:

我需要在我的初始控制器上显示一个视图,并在 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)
        }
    }
}

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
}

视图控制器

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 未设置,即使控制器已经初始化。

【问题讨论】:

    标签: swift firebase-authentication appdelegate email-verification uiscenedelegate


    【解决方案1】:

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

    // 场景代理

    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)
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2013-01-20
      • 1970-01-01
      • 1970-01-01
      • 2013-03-08
      • 2012-12-23
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多