Sunkas
Sunkas

Reputation: 9590

Copy file to root of Xcode project when building

I am trying to implement Google Analytics (GA) in my iOS apps. I have two different targets that have different tracking-ids for GA. GA requires a GoogleService-Info.plist (cannot be renamed) file to be placed in the root of the app folder structure. This file contains the tracking-id. However, since I have two different targets I need to have two different tracking-ids.

I cannot have two file named to files with the same name with different targets.

So, is there a way to either copy another file into the root in the build process. Have tried this and similar but does not seem to work:

enter image description here

Any suggestions?

Upvotes: 3

Views: 3837

Answers (2)

Aleksey Potapov
Aleksey Potapov

Reputation: 3783

The script:

PLIST_FILE="CustomGoogleService-Info.plist"
PLIST_PATH="${PROJECT_DIR}/path/To/Plist/Here/${PLIST_FILE}"
cp "${PLIST_PATH}" "${CONFIGURATION_BUILD_DIR}/${CONTENTS_FOLDER_PATH}/GoogleService-Info.plist"

Instructions:

  • Add this script to the end of your Build Phases
  • Change name CustomGoogleService-Info.plist to your own
  • Change pathToPlistHere to the correct path where CustomGoogleService-Info.plist is located (starting from your project directory)
  • You should not have GoogleService-Info.plist file in your project - the script will create it for you.
  • Grab a coffee while it is running

You may be interested in reading Apple's Xcode Build Setting Reference

Alternatively: You could have 2 files with the name GoogleService-Info.plist, but keep each in separate directory. Then you could add each to the corresponding target. Without any script.

Upvotes: 11

Sunkas
Sunkas

Reputation: 9590

I did correctly all along, however Destination should be set to Wrapper and subpath empty. No need to have them in the target either.

This one explained the Destination options: xcode copy files build phase - what do the destination options mean exactly?

Upvotes: 1

Related Questions