【问题标题】:Use of unresolved identifier 'FIRAuth' (Swift 2, Firebase 3.x)使用未解析的标识符“FIRAuth”(Swift 2、Firebase 3.x)
【发布时间】:2016-09-17 15:31:19
【问题描述】:

正在更新到新的 Firebase。创建了一个新的登录 VC,一切正常,没有错误。

尝试复制这个新教程:https://codelabs.developers.google.com/codelabs/firebase-ios-swift/index.html?index=..%2F..%2Findex#0

现在我突然在我的 VC 中收到错误 Use of unresolved identifier 'FIRAuth' 。

我已经尝试重新安装 pods 文件并且没有任何运气,似乎有时如果它添加“import Firebase”然后删除它,应用程序将编译,似乎没有押韵或原因它有时有效,有时无效:

这是我的代码:

import UIKit
import FirebaseAuth


class SignInViewController: UIViewController {

@IBOutlet weak var emailField: UITextField!

@IBOutlet weak var passwordField: UITextField!

override func viewDidAppear(animated: Bool) {
    if let user = FIRAuth.auth()?.currentUser { //error here 
        self.signedIn(user)
    }
}

@IBAction func didTapSignIn(sender: AnyObject) {
    // Sign In with credentials.
    let email = emailField.text
    let password = passwordField.text
    FIRAuth.auth()?.signInWithEmail(email!, password: password!) { //error here (user, error) in
        if let error = error {
            print(error.localizedDescription)
            return
        }
        self.signedIn(user!)
    }
}
@IBAction func didTapSignUp(sender: AnyObject) {
    let email = emailField.text
    let password = passwordField.text
    FIRAuth.auth()?.createUserWithEmail(email!, password: password!) { // error here(user, error) in
        if let error = error {
            print(error.localizedDescription)
            return
        }
        self.setDisplayName(user!)
    }
}

func setDisplayName(user: FIRUser) {
    let changeRequest = user.profileChangeRequest()
    changeRequest.displayName = user.email!.componentsSeparatedByString("@")[0]
    changeRequest.commitChangesWithCompletion(){ (error) in
        if let error = error {
            print(error.localizedDescription)
            return
        }
        self.signedIn(FIRAuth.auth()?.currentUser) //error here
    }
}

@IBAction func didRequestPasswordReset(sender: AnyObject) {
    let prompt = UIAlertController.init(title: nil, message: "Email:", preferredStyle: UIAlertControllerStyle.Alert)
    let okAction = UIAlertAction.init(title: "OK", style: UIAlertActionStyle.Default) { (action) in
        let userInput = prompt.textFields![0].text
        if (userInput!.isEmpty) {
            return
        }
        FIRAuth.auth()?.sendPasswordResetWithEmail(userInput!) { //error here (error) in
            if let error = error {
                print(error.localizedDescription)
                return
            }
        }
    }
    prompt.addTextFieldWithConfigurationHandler(nil)
    prompt.addAction(okAction)
    presentViewController(prompt, animated: true, completion: nil);
}

func signedIn(user: FIRUser?) {
    MeasurementHelper.sendLoginEvent()

    AppState.sharedInstance.displayName = user?.displayName ?? user?.email
    AppState.sharedInstance.photoUrl = user?.photoURL
    AppState.sharedInstance.signedIn = true
    NSNotificationCenter.defaultCenter().postNotificationName(Constants.NotificationKeys.SignedIn, object: nil, userInfo: nil)
   // performSegueWithIdentifier(Constants.Segues.SignInToFp, sender: nil)
}

}

有谁知道为什么会发生这种情况?

【问题讨论】:

  • 看到这个链接它可以帮助你stackoverflow.com/questions/34950777/…
  • 感谢 Anbu,刚刚看了一下那个问题,我的 SignInViewController 已经正确连接到目标。我不知道还有什么问题。

标签: ios firebase swift2 xcode7 firebase-authentication


【解决方案1】:

现在的解决方案,在 Swift 4.2 中,它只是抱怨“Auth”而不是“FIRAuth”说“使用未解析的标识符 Auth”:

请注意,有两个不同的导入。 import Firebaseimport FirebaseAuth

第一个版本在大多数情况下就足够了,但有时编译器会感到困惑,添加第二个版本有助于解决问题。

【讨论】:

    【解决方案2】:

    looks like 现在只是“Auth”而不是“FIRAuth”

    【讨论】:

      【解决方案3】:

      您必须在 pod 文件上添加 pod 'Firebase/Auth',将 Firebase 和 FirebaseAuth 导入您的控制器,现在要使用 Auth 不是 FIRAuth.auth(),是 Auth.auth().signInAnonymously 并且工作正常。

      【讨论】:

        【解决方案4】:

        现在它已从“FIRAuth”重命名为“Auth”

        【讨论】:

          【解决方案5】:

          首先我们需要在 podfile 中添加 firebase Auth 的 pod

          pod 'Firebase/Auth'

          然后我们需要使用 'pod install' 运行终端

          根据firebase Doc,我们需要在我们的视图控制器上添加import firebase。但这不能解决您的问题。您需要添加import FirebaseAuth。这将消除错误。

          【讨论】:

            【解决方案6】:

            2016 年 12 月 26 日更新为 Swift 3Firebase 3.11.0
            添加到 Podfile

            pod 'Firebase/Auth'
            

            在您需要使用身份验证的地方,只需

            import Firebase
            

            清理并重建,您将清除错误。

            此解决方案来自 Google。 https://firebase.google.com/docs/auth/ios/password-auth

            【讨论】:

              【解决方案7】:

              对于未来的读者:

              确保在您的Podfile 中包含以下内容:

              pod 'Firebase/Auth'

              安装 pod 后,使用:

              import FirebaseAuth

              这就是为我解决的问题。

              【讨论】:

                【解决方案8】:

                我更新了 Cocoapods 并运行了 pod update,它解决了我的所有问题

                【讨论】:

                • 我需要知道你使用了什么命令。当然,它被选为答案,但你所做的会很棒。这是正确的。
                【解决方案9】:

                在 UIViewController 中使用 Firebase 时,我确保导入 Firebase,然后清理缓存/构建 (cmd + shift + k),然后构建 (cmd + b)。

                似乎可行,但每次构建时我都必须重做该过程。

                编辑

                如果第一次清洁不起作用,请继续清洁直到它起作用。不是完美的解决方案,但它有效。

                【讨论】:

                  【解决方案10】:

                  MeasurementHelper.sendLoginEvent()

                  AppState.sharedInstance.displayName = user?.displayName ?? user?.email
                  

                  AppState 是一个未知的

                  【讨论】:

                    【解决方案11】:

                    删除此导入:

                    导入 FirebaseAuth

                    改为添加此语句。这对我有用。

                    导入 Firebase

                    【讨论】:

                    • 别介意忽略这个答案,当我随机运行它时我仍然遇到错误,我清理项目并重建它并且错误消失了。但是一段时间后我遇到了同样的问题。
                    【解决方案12】:

                    添加“导入 Firebase”并按 cmd + B

                    【讨论】:

                      猜你喜欢
                      • 1970-01-01
                      • 1970-01-01
                      • 1970-01-01
                      • 1970-01-01
                      • 1970-01-01
                      • 2016-04-29
                      • 1970-01-01
                      • 2016-06-09
                      • 2021-04-26
                      相关资源
                      最近更新 更多