使用未解决的标识符'FIRAuth' (Swift 2,Firebase 3.x)

22

更新到新的Firebase,创建了一个新的登录视图控制器。一切都很顺利,没有错误。

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

现在突然间,我的视图控制器中到处都是“未解决的标识符‘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)
}

}

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


请查看此链接以获取帮助:https://dev59.com/zJPfa4cB1Zd3GeqPBU21 - Anbu.Karthik
谢谢Anbu,我刚刚看了一下那个问题,我的SignInViewController已经正确连接到目标了。对于问题的其他可能原因,我感到有些困惑。 - ryanbilak
12个回答

52

对于未来的读者:

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

pod 'Firebase/Auth'

安装 pods 后,使用以下代码:

import FirebaseAuth

这是我解决问题的方法。


7

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


我需要知道你使用了哪些命令。当然,这被选为答案,但知道你具体做了什么会更好。这是正确的做法。 - Vijay Kumar Kanta

5

您需要在Pod文件中添加pod 'Firebase/Auth',并将Firebase和FirebaseAuth导入到您的控制器中。现在使用Auth不再是FIRAuth.auth(),而是Auth.auth().signInAnonymously,这样就可以正常工作。


5

更新于2016/12/26,使用Swift 3Firebase 3.11.0
添加到Podfile中

pod 'Firebase/Auth'

在您的情况下,您需要使用Auth,只需:
import Firebase

清理并重新构建,这将清除错误。此解决方案来自Google。 https://firebase.google.com/docs/auth/ios/password-auth

3

添加 "import Firebase" 并按下 cmd + B


3
在UIViewController中使用Firebase时,我会确保导入Firebase,然后清除缓存/构建(cmd + shift + k),然后再构建(cmd + b)。
看起来是行得通的,但每次构建都需要重新执行此过程。
编辑:如果第一次清除没有起作用,请继续清除,直到它起作用。虽然不是完美的解决方案,但它是有效的。

2

现在在Swift 4.2中的解决方案,当它抱怨简单地使用“Auth”而不是“FIRAuth”时会说“使用未解决的标识符Auth”:

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

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


2

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

pod 'Firebase/Auth'

然后我们需要在终端中运行 'pod install'

根据firebase文档,我们需要在viewcontroller中添加import firebase。但这并不能解决你的问题。你需要添加import FirebaseAuth。这样就可以解决错误了。


1
现在它已经被从 "FIRAuth" 重命名为 "Auth"。

1

移除这个导入语句:

import FirebaseAuth

替换为以下语句。此方法适用于我。

import Firebase


没关系,不用理会这个答案,我在随机运行时仍然遇到错误,我清理了项目并重新构建它,错误消失了。但是过一段时间后,我又遇到了同样的问题。 - user1457303

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接