ios - iOS(Swift) 中未显示 Firebase Auth UI

标签 ios swift firebase firebase-authentication

我想试用 Firebase Auth UI,我一直在关注官方文档。这些文档(至少对于 iOS)非常困惑,有些已经过时(不正确的导入命名等)。现在我只想在我的测试应用程序开始时显示 Firebase AuthUI 屏幕。我在下面试过

import UIKit
import FirebaseAuthUI
import FirebaseFacebookAuthUI
import FirebaseTwitterAuthUI

class ViewController: UIViewController {

    override func viewDidLoad() {
        super.viewDidLoad()

    }

    override func viewWillAppear(_ animated: Bool) {
        super.viewWillAppear(animated)

        if let authUI = FUIAuth.defaultAuthUI() {
            authUI.delegate = self

            // Setup login provider ( Need to import these seperately )
            authUI.providers = [ FUIFacebookAuth(), FUITwitterAuth() ]

            let authViewController = authUI.authViewController()
            present(authViewController, animated: true, completion: {})
        }
    }
    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
    }

}

extension ViewController: FUIAuthDelegate {
    func authUI(_ authUI: FUIAuth, didSignInWith authDataResult: AuthDataResult?, error: Error?) {
        // (Keep empty for now)
    }
}

authViewController 没有以某种方式呈现,我不知道为什么。我尝试将身份验证内容同时放在 viewDidLoadviewWillAppear 中,但没有任何效果。

我也试过谷歌并查看其他教程等,但没有解决我的问题。

如果有人能指出我的错误或至少指出正确的方向,那就太好了。谢谢

Edit1:我尝试在当前放入完成处理程序但它没有运行,但 authViewController 不是 nil

最佳答案

您正试图在您的 viewWillAppear 中显示一个新的 Controller /屏幕,这意味着您的屏幕尚未完全加载。所以应该有一个来自 Xcode 的警告,看起来像这样:

Warning: Attempt to present on whose view is not in the window hierarchy!

现在,将代码移到 viewDidAppear 中,如下所示:

override func viewDidAppear(_ animated: Bool) {
    super.viewDidAppear(animated)

    if let authUI = FUIAuth.defaultAuthUI() {
        authUI.delegate = self

        // Setup login provider ( Need to import these seperately )
        authUI.providers = [ FUIFacebookAuth(), FUITwitterAuth() ]

        let authViewController = authUI.authViewController()
        present(authViewController, animated: true, completion: {})
    }
}

这应该有效。 :)

关于ios - iOS(Swift) 中未显示 Firebase Auth UI,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51345736/

相关文章:

ios - 在 UITabBar iOS 中更改项目颜色

iphone - 使用 Storyboard将 UITabViewController 放入 UINavigationViewController 中

ios - 警告 : attempt to present whose view is not in the window hierarchy

ios - CollectionView 在新帖子后复制数据

ios - 如何在 UIView 中自定义此图像选项卡,使用 @IBDesignable 和 @IBInspectable 更好

ios - 如何摆脱数组中的多组括号

ios - 如何使用 Swift 4 在 UITextfield 中实现 Material chips

swift - 从点数组快速绘制平滑曲线

android - Google map 未在 Firebase 测试实验室中显示

javascript - 如何使用 DocumentChange.newIndex 重新排序 Firestore 'modified' 更改?