iOS Firebase Connection Nothing Happens

Hi,

I have no problem to connect to Firebase or access Firestore from web or Android.

But from iOS, when compiled like an app through Xcode it doesn’t work. It’s like the connection can’t be established. It’s what I suppose because I’m not able to see any logs. I took 3 days on this issue and can’t solve it… Here is what I’ve done so far :

On Firebase : created an iOS app too (required ? or just web app ?)

  1. iOS app associated with the bundle id on Firestore (not sure it’s useful)
  2. GoogleService-Info.plist added to the Xcode project

Try to log in

  1. various tests carried out, it bugs at the signInAnonymously(auth) level it can’t be done.
  2. On Firebase, I have activated the anonymous connection and I have the users (my tests) in anonymous connection who appear in the list of users

HTTPS

  1. I have set NSAppTransportSecurity in the Info.plist for Xcode with NSAllowsArbitraryLoads in true

FYI : I think the problem is an https problem because I have 2 bugs: Firebase, I have nothing in result or in console logs so clearly there are 0 connections. I have another connection to a Google API via a GCP call which does not succeed but there the problem is explicit it is because of the CORS (I have console logs for that one).

DEBUG

  1. I tested setLogLevel(“debug”); which works great in web/android but nothing on iOS

If someone has a sample project which works with Firestore or know a link to it, I’ll be very happy to try it.

Thanks in advance for your help,
Thomas

I found the solution, it’s quite easy actually and it’s not related to CORS finally.

Here is the solution to add in your Firebase configuration :slight_smile:

Check where my comment starts with HERE NEW CODE and the imports :slight_smile:

import { initializeApp } from "firebase/app";
import {
  initializeFirestore,
  persistentLocalCache,
  persistentSingleTabManager,
  CACHE_SIZE_UNLIMITED,
} from "firebase/firestore";
import {
  getAuth,
  setPersistence,
  browserLocalPersistence,
  indexedDBLocalPersistence,
  initializeAuth,
  Auth,
} from "firebase/auth";
import { Capacitor } from "@capacitor/core";

const firebaseConfig = {
  apiKey: process.env.REACT_APP_FIREBASE_API_KEY,
  authDomain: process.env.REACT_APP_FIREBASE_AUTH_DOMAIN,
  projectId: process.env.REACT_APP_FIREBASE_PROJECT_ID,
  storageBucket: process.env.REACT_APP_FIREBASE_STORAGE_BUCKET,
  messagingSenderId: process.env.REACT_APP_FIREBASE_MESSAGING_SENDER_ID,
  appId: process.env.REACT_APP_FIREBASE_APP_ID,
  measurementId: process.env.REACT_APP_FIREBASE_MEASUREMENT_ID,
};

// Initialisation de Firebase
const app = initializeApp(firebaseConfig);
console.log("Firebase Initialised");

// Configuration Firestore avec persistance des données en cache
const firestore = initializeFirestore(app, {
  localCache: persistentLocalCache({
    tabManager: persistentSingleTabManager(undefined),
    cacheSizeBytes: CACHE_SIZE_UNLIMITED,
  }),
});


// HERE NEW CODE : Auth differently from Web or Device (Native)
let auth: Auth;
if (Capacitor.isNativePlatform()) {
  // Sur mobile (iOS/Android), utiliser IndexedDB
  auth = initializeAuth(app, {
    persistence: indexedDBLocalPersistence
  });
  console.log("[Firebase] 📱 Auth set up for IndexedDB (mobile).");
} else {
  // Sur web, utiliser browserLocalPersistence
  auth = getAuth(app);
  setPersistence(auth, browserLocalPersistence)
    .then(() => {
      console.log("Persistance Firebase Auth set up for local (web).");
    })
    .catch((error) => {
      console.error(
        "Error while configuring Firebase Auth persistence:",
        error
      );
    });
}

export default app;
export { firestore, auth };

source : Auth does not work in ionic apps on iphone devices or sims · Issue #5552 · firebase/firebase-js-sdk · GitHub

1 Like

Make sure your GoogleService-Info.plist is properly added to the project (not just the folder but in Xcode under “Copy Bundle Resources”). Also, double-check that Firebase initialization is happening correctly in AppDelegate.swift.