繁体   English   中英

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

[英]Use of unresolved identifier 'FIRAuth' (Swift 2, Firebase 3.x)

更新到新的firebase。 创建了一个新的登录VC,一切都运行良好,没有错误。

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

现在突然间我收到错误使用未解析的标识符'FIRAuth'到我的VC。

我已经尝试重新安装pods文件,并没有任何运气,似乎有时如果它添加“导入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)
}

}

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

对于未来的读者:

确保在Podfile包含以下Podfile

pod 'Firebase/Auth'

安装pod后,使用:

import FirebaseAuth

这就是为我解决的问题。

我更新了Cocoapods并运行了pod更新,它修复了我的所有问题

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

pod 'Firebase/Auth'

在您的位置,您需要使用Auth

import Firebase

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

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

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

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

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

似乎工作,但每次我建立时我都要重做这个过程。

编辑

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

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

pod'Firebase / Auth'

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

根据firebase文档,我们需要在viewcontroller上添加import firebase 。但它不能解决你的问题。你需要添加导入FirebaseAuth 。这将删除错误。

删除此导入:

导入FirebaseAuth

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

导入Firebase

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

看起来它现在只是“Auth”而不是“FIRAuth”

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

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

第一个是大部分时间都足够了,但有时编译器会感到困惑,添加第二个版本有助于清除问题。

MeasurementHelper.sendLoginEvent()

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

AppState是一个不知情的人

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM