简体   繁体   中英

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

Updating to the new firebase. Created a new signin VC and everything was working fine in terms of no errors.

Trying to replicate this new tutorial: https://codelabs.developers.google.com/codelabs/firebase-ios-swift/index.html?index=..%2F..%2Findex#0

Now all of a sudden I'm getting the error Use of unresolved identifier 'FIRAuth' all over my VC.

I've tried re-installing the pods file and haven't had any luck, it seems that sometimes if it add "import Firebase" then remove it the app will compile, it seems there is no rhyme or reason for why it works sometimes and other times it doesn't:

Here's my code:

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)
}

}

Does anyone have any idea why this would be happening?

For future readers:

Make sure to have include the following in your Podfile :

pod 'Firebase/Auth'

After installing the pods, use:

import FirebaseAuth

This is what solved it for me.

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

Updated 2016/12/26 with Swift 3 and Firebase 3.11.0
Add to Podfile

pod 'Firebase/Auth'

In your place you need to use Auth, just

import Firebase

Clean and rebuild, you will clear error.

This solution is referred from 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.

When using Firebase in a UIViewController I make sure to import Firebase and after that I clean the cache/build (cmd + shift + k) and then build (cmd + b).

Seems to work but I have to redo the process every time I build.

EDIT

If it doesn't work the first clean, just keep on cleaning until it does. Not the perfect solution but it works.

first we need to add pod of firebase Auth in podfile

pod 'Firebase/Auth'

Then we need to run the terminal with ' pod install '

According to the firebase Doc,we need to add import firebase on our viewcontroller .but it will not solve your problem.you need to add import FirebaseAuth .This will remove the error.

Remove this import:

import FirebaseAuth

Add this statement instead. This worked for me.

import Firebase

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

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

Solution now, in Swift 4.2, where it complains about simply "Auth" and not "FIRAuth" saying "Use of unresolved identifier Auth":

Note there are two distinct imports. import Firebase and import FirebaseAuth

The first was sufficient most of the time, but sometimes the compiler gets confused and adding the second version helps clear things up.

MeasurementHelper.sendLoginEvent()

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

AppStateis an unidetified

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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