Authenticate with Firebase Anonymously on Apple Platforms Stay organized with collections Save and categorize content based on your preferences.
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
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.- In Xcode, with your app project open, navigate toFile > Add Packages.
- When prompted, add the Firebase Apple platforms SDK repository:
- Choose theFirebase Authentication library.
- Add the
-ObjCflag to theOther Linker Flags section of your target's build settings. - When finished, Xcode will automatically begin resolving and downloading your dependencies in the background.
Note: New projects should use the default (latest) SDK version, but you can choose an older version if needed.https://github.com/firebase/firebase-ios-sdk.git
- If you haven't yet connected your app to your Firebase project, do so from theFirebase console.
- Enable anonymous auth:
- In theFirebase console, open theAuth section.
- On theSign-in Methods page, enable theAnonymous sign-in method.
- 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:
- Import the
FirebaseCoremodule 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;// ...
- Configure a
FirebaseAppshared 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];
- If you're using SwiftUI, you must create an application delegate and attach it to your
Appstruct viaUIApplicationDelegateAdaptororNSApplicationDelegateAdaptor. 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()}}}}
- Call the
signInAnonymouslyWithCompletion:method:Swift
Auth.auth().signInAnonymously{authResult,errorin// ...}
Objective-C
[[FIRAuthauth]signInAnonymouslyWithCompletion:^(FIRAuthDataResult*_NullableauthResult,NSError*_Nullableerror){// ...}];
- If the
signInAnonymouslyWithCompletion:method completes without error, you can get the anonymous user's account data from theFIRAuthDataResultobject:Swift
guardletuser=authResult?.userelse{return}letisAnonymous=user.isAnonymous// trueletuid=user.uid
Objective-C
FIRUser*user=authResult.user;BOOLisAnonymous=user.anonymous;// YESNSString*uid=user.uid;
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:
- When the user signs up, complete the sign-in flow for the user's authentication provider up to, but not including, calling one of the
FIRAuth.signInWithmethods. For example, get the user's Google ID token, Facebook access token, or email address and password. Get an
FIRAuthCredentialfor 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];
Pass the
FIRAuthCredentialobject 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.
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.