Authenticate with Firebase Anonymously on Apple Platforms

You can useFirebase Authentication to create and use temporary anonymous accountsto authenticate with Firebase. These temporary anonymous accounts can be used toallow users who haven't yet signed up to your app to work with data protectedby security rules. If an anonymous user decides to sign up to your app, you canlink their sign-in credentials to the anonymousaccount so that they can continue to work with their protected data infuture sessions.

Before you begin

  1. Use Swift Package Manager to install and manage Firebase dependencies.

    Visitour installation guide to learn about the different ways you can add Firebase SDKs to your Apple project.
    1. In Xcode, with your app project open, navigate toFile > Add Packages.
    2. When prompted, add the Firebase Apple platforms SDK repository:
    3.   https://github.com/firebase/firebase-ios-sdk.git
      Note: New projects should use the default (latest) SDK version, but you can choose an older version if needed.
    4. Choose theFirebase Authentication library.
    5. Add the-ObjC flag to theOther Linker Flags section of your target's build settings.
    6. When finished, Xcode will automatically begin resolving and downloading your dependencies in the background.
  2. If you haven't yet connected your app to your Firebase project, do so from theFirebase console.
  3. Enable anonymous auth:
    1. In theFirebase console, open theAuth section.
    2. On theSign-in Methods page, enable theAnonymous sign-in method.
    3. Optional: If you've upgraded your project toFirebase Authentication with Identity Platform, you can enable automatic clean-up. When you enable this setting, anonymous accounts older than 30 days will be automatically deleted. In projects with automatic clean-up enabled, anonymous authentication will no longer count toward usage limits or billing quotas. SeeAutomatic clean-up.

Authenticate with Firebase anonymously

When a signed-out user uses an app feature that requires authentication withFirebase, sign in the user anonymously by completing the following steps:

  1. Import theFirebaseCore module in yourUIApplicationDelegate, as well as any otherFirebase modules your app delegate uses. For example, to useCloud Firestore andAuthentication:

    SwiftUI

    importSwiftUIimportFirebaseCoreimportFirebaseFirestoreimportFirebaseAuth// ...

    Swift

    importFirebaseCoreimportFirebaseFirestoreimportFirebaseAuth// ...

    Objective-C

    @importFirebaseCore;@importFirebaseFirestore;@importFirebaseAuth;// ...
  2. Configure aFirebaseApp shared instance in your app delegate'sapplication(_:didFinishLaunchingWithOptions:) method:

    SwiftUI

    // Use Firebase library to configure APIsFirebaseApp.configure()

    Swift

    // Use Firebase library to configure APIsFirebaseApp.configure()

    Objective-C

    // Use Firebase library to configure APIs[FIRAppconfigure];
  3. If you're using SwiftUI, you must create an application delegate and attach it to yourApp struct viaUIApplicationDelegateAdaptor orNSApplicationDelegateAdaptor. You must also disable app delegate swizzling. For more information, see theSwiftUI instructions.

    SwiftUI

    @mainstructYourApp:App{// register app delegate for Firebase setup@UIApplicationDelegateAdaptor(AppDelegate.self)vardelegatevarbody:someScene{WindowGroup{NavigationView{ContentView()}}}}
  4. Call thesignInAnonymouslyWithCompletion: method:

    Swift

    Auth.auth().signInAnonymously{authResult,errorin// ...}

    Objective-C

    [[FIRAuthauth]signInAnonymouslyWithCompletion:^(FIRAuthDataResult*_NullableauthResult,NSError*_Nullableerror){// ...}];
  5. If thesignInAnonymouslyWithCompletion: method completes without error, you can get the anonymous user's account data from theFIRAuthDataResult object:

    Swift

    guardletuser=authResult?.userelse{return}letisAnonymous=user.isAnonymous// trueletuid=user.uid

    Objective-C

    FIRUser*user=authResult.user;BOOLisAnonymous=user.anonymous;// YESNSString*uid=user.uid;
To protect your project from abuse, Firebase limits the number of newemail/password and anonymous sign-ups that your application can have from thesame IP address in a short period of time. You can request and scheduletemporary changes to this quota from theFirebase console.

Convert an anonymous account to a permanent account

When an anonymous user signs up to your app, you might want to allow them tocontinue their work with their new account—for example, you might want tomake the items the user added to their shopping cart before they signed upavailable in their new account's shopping cart. To do so, complete the followingsteps:

  1. When the user signs up, complete the sign-in flow for the user's authentication provider up to, but not including, calling one of theFIRAuth.signInWith methods. For example, get the user's Google ID token, Facebook access token, or email address and password.
  2. Get anFIRAuthCredential for the new authentication provider:

    Google Sign-In
    Swift
    guardletauthentication=user?.authentication,letidToken=authentication.idTokenelse{return}letcredential=GoogleAuthProvider.credential(withIDToken:idToken,accessToken:authentication.accessToken)
    Objective-C
    FIRAuthCredential*credential=[FIRGoogleAuthProvidercredentialWithIDToken:result.user.idToken.tokenStringaccessToken:result.user.accessToken.tokenString];
    Facebook Login
    Swift
    letcredential=FacebookAuthProvider.credential(withAccessToken:AccessToken.current!.tokenString)
    Objective-C
    FIRAuthCredential*credential=[FIRFacebookAuthProvidercredentialWithAccessToken:[FBSDKAccessTokencurrentAccessToken].tokenString];
    Email-password sign-in
    Swift
    letcredential=EmailAuthProvider.credential(withEmail:email,password:password)
    Objective-C
    FIRAuthCredential*credential=[FIREmailAuthProvidercredentialWithEmail:emailpassword:password];
  3. Pass theFIRAuthCredential object to the sign-in user'slinkWithCredential:completion: method:

    Swift
    user.link(with:credential){authResult,errorin// ...}}
    Objective-C
    [[FIRAuthauth].currentUserlinkWithCredential:credentialcompletion:^(FIRAuthDataResult*result,NSError*_Nullableerror){// ...}];

If the call tolinkWithCredential:completion: succeeds, the user's new account canaccess the anonymous account's Firebase data.

This technique can also be used tolink any two accounts.

Automatic clean-up

If you've upgraded your project toFirebase Authentication with Identity Platform, you can enable automatic clean-up in theFirebase console. When you enable this feature you allow Firebase to automatically delete anonymous accounts older than 30 days. In projects with automatic clean-up enabled, anonymous authentication will not count toward usage limits or billing quotas.

  • Any anonymous accounts created after enabling automatic clean-up might be automatically deleted any time after 30 days post-creation.
  • Existing anonymous accounts will be eligible for automatic deletion 30 days after enabling automatic clean-up.
  • If you turn automatic clean-up off, any anonymous accounts scheduled to be deleted will remain scheduled to be deleted.
  • If you "upgrade" an anonymous account by linking it to any sign-in method, the account will not get automatically deleted.

If you want to see how many users will be affected before you enable this feature, and you'veupgraded your project toFirebase Authentication with Identity Platform, you can filter byis_anon inCloudLogging.

Next steps

Now that users can authenticate with Firebase, you can control their access todata in your Firebase database usingFirebase rules.

Except as otherwise noted, the content of this page is licensed under theCreative Commons Attribution 4.0 License, and code samples are licensed under theApache 2.0 License. For details, see theGoogle Developers Site Policies. Java is a registered trademark of Oracle and/or its affiliates.

Last updated 2026-02-18 UTC.