Use different GoogleService-Info.plist for single project in xcode using swift4
I got the solution. I renamed GoogleService-Info.plists files by its respective environments. And Added those files in Build Phases -> Copy Bundle Resources. Then added the following code as per the environment selected
guard let plistPath = Bundle.main.path(forResource: "nameOfTheGoogleService-Info.plistFileAsPerTheEnvironment", ofType: "plist"),
let options = FirebaseOptions(contentsOfFile: plistPath)
else { return }
if FirebaseApp.app() == nil{
FirebaseApp.configure(options: options)
}
Minor change is that, when user changes the app's environment, then he should remove app from background and open it again. Then the AppDeleagte takes the path of respective GoogleService-Info.plist as per its environment selected
Have two GoogleService-Info.plist in one target in XCode
I use the following solution:
Create a GoogleService-CONFIGURATION-Info.plist file for every XCode configuration. Don't link the files to a target.
Add a Run Script Build Phase:
cp ${SRCROOT}/Resources/Firebase/GoogleService-${CONFIGURATION}-Info.plist ${CONFIGURATION_BUILD_DIR}/${UNLOCALIZED_RESOURCES_FOLDER_PATH}/GoogleService-Info.plist
How to use two different GoogleService-info.plist file in ios swift project for dev n prod?
For this case you need use different Targets with diff bundle id in project for Dev and Prod.
After that you will can link GoogleService-info.plist
for this targets. And also I recommend create different folders in project directory
Look at pictures. The first you create targets Prod and Dev
After create diff folders in project dict let's call them GoogleProd and GoogleDev and copy your .plist
file to diff folders and drag to project. First file you need link with TestProd target
And the second file link with TestDev target
The final project directory will look like
How to set different Google-Service-info-plist file according to environment in iOS Swift?
struct Configuration {
static var environment: Environment = {
return Environment.init(rawValue: Bundle.main.object(forInfoDictionaryKey: "Configuration") as! String)!
}()
static var documentsDir: String {
guard let dir = NSSearchPathForDirectoriesInDomains(.documentDirectory, .userDomainMask, true).first else { fatalError("There was something wrong finding the documents directory") }
return dir
}
}
enum Environment: String {
case local = "Local"
case production = "Production"
var googlePlistFileName: String {
switch self {
case .local:
return "GoogleServiceMobileLocal"
case .production:
return "GoogleServiceMobileProduction"
}
}
}
I added one key in info-plist file
Could not find a valid GoogleService-Info.plist after handle different configuration in Build phrase
I believe there are multiple issues in your approach
Issue 1:
I believe simply copying the file to the folder will not help, you need to add the file to project and change its target membership to the specific target you are running. You need to add the file to Copy Bundle Resources in build phase of specific target you are running with.
Issue 2:
Also though you have changed the filename of GoogleService-info.plist
name Firebase is still looking for GoogleService-info.plist
and not your renamed file hence the crash.
As mentioned here github.com/firebase/quickstart-ios/issues/5 you can not change the filename, file has to be named as GoogleService-info.plist. So copying entire file would not work.
Probable Solution:
Rather than copying the file what you can do is have a empty plist file named GoogleService-info.plist
make sure you have its target membership correctly ticked and also added to Copy Bundle Resource of your target. Copy other GoogleService-Info-Configuration.plist file as well to your project.
Then, in your run script, read the content from specific GoogleService-Info-{Configuration}.plist and simply copy the content of file to your GoogleService-info.plist
Using something like
cat "${SRCROOT}/Test/Resourses/Firebase/GoogleService-Info-$CONFIGURATION.plist" > "${SRCROOT}/Test/Resourses/Firebase/GoogleService-Info.plist"
Or as OP mentioned in his comment use
cp "${SRCROOT}/Turf/Resourses/Firebase/GoogleService-Info-$CONFIGURATION.plist" "${SRCROOT}/Turf/Resourses/Firebase/GoogleService-Info.plist"
I have not tested above pasted shell Script, though it should give you fairly simple idea how to approach the problem.
How to add .plist file to all targets in Xcode?
Select the GoogleService-Info.plist in the project navigator (the file hierarchy on the left side of Xcode). Then, if it isn't open yet, open the utilities bar (click the top-right most button in Xcode). You will see a section 'Target Membership'. Check all the checkboxes to add the .plist to all targets.
In most cases it is enough to just add the file to your project.
Related Topics
How to Import Modules Without an Xcode Project in Swift
How to Retrieve a Value from Dictionary in Swift 3
Converting Audiobuffer to Cmsamplebuffer with Accurate Cmtime
Swift Alternative to Respondstoselector:
Swift "Is" Operator with Type Stored in Variable
What Does a "Do Statement" Without Catch Block Mean
Swiftui Widget iOS 14 Gradient Issue
Combining Custom Property Wrapper with @Published
Uiswipegesturerecognizer Doesn't Recognize Swipe Gesture Initiated Outside the View
Anyobject Try Cast to Equatable
How Does Let X Where X.Hassuffix("Pepper") Work
Updating a @Published Variable Based on Changes in an Observed Variable
How to Process an Array of Task Asynchronously with Swift Combine
Use Different Googleservice-Info.Plist for Single Project in Xcode Using Swift4
Swift Protocol for String Interpolation
Typecast Unsafemutablepointer<Void> to Unsafemutablepointer<#Struct Type#>
Swift Combine: Using Timer Publisher in an Observable Object