It is definitely possible to access multiple Firestore databases (or other Firebase resources) in a single application. But only one of them can be initialized from the GoogleService-Info.plist
. The other one(s) you will have to initialize from within your code, based on the information in the secondary GoogleService-Info.plist
.
The basic approach for this is to first create a FirebaseOptions
object with the configuration data of the second project:
// Configure with manual options.
let secondaryOptions = FirebaseOptions(googleAppID: "1:27992087142:ios:2a4732a34787067a", gcmSenderID: "27992087142")
secondaryOptions.bundleID = "com.google.firebase.devrel.FiroptionConfiguration"
secondaryOptions.apiKey = "AIzaSyBicqfAZPvMgC7NZkjayUEsrepxuXzZDsk"
secondaryOptions.clientID = "27992087142-ola6qe637ulk8780vl8mo5vogegkm23n.apps.googleusercontent.com"
secondaryOptions.databaseURL = "https://myproject.firebaseio.com"
secondaryOptions.storageBucket = "myproject.appspot.com"
And then use that to initialize a secondary App
object to get the Firebase service(s) you need:
// Configure an alternative FIRApp.
FirebaseApp.configure(name: "secondary", options: secondaryOptions)
// Retrieve a previous created named app.
guard let secondary = FirebaseApp.app(name: "secondary")
else { assert(false, "Could not retrieve secondary app") }
let secondaryDb = Firestore.firestore(app: secondary)
Also see: