Use User-Defined build settings in custom .plist f

2019-02-01 09:05发布

I have different build configurations (Debug, Stage, Prod) defined for my app and I use User-Defined build settings:

enter image description here

to set up Facebook login and other stuff in Info.plist file:

enter image description here

In this scenario the $(USER_DEFINED_SETTINGS) notation does work.

When I tried to set up Google SignIn, which requires using additional .plist file (GoogleService-Info.plist), and I used User-Defined settings in the same way I do in the Info.plist file, it doesn't work.

How can I use User-Defined settings in custom .plist files? If I can't, how can I workaround this?

3条回答
霸刀☆藐视天下
2楼-- · 2019-02-01 09:24

It's NOT possible to use User-Defined settings in custom .plist file, so you have to handle this in the other way.

Although, you can copy your custom .plist file to the right place:

  1. Create a new folder (for example: GoogleServiceInfoPlists).
  2. Copy there all .plist files for each environment (for example: GoogleService-Info-Debug.plist, GoogleService-Info-Stage.plist and GoogleService-Info-Prod.plist).
  3. Add new Run Script Phase (Xcode: Target->Build Phases->"+" button).
  4. Use script below to copy (replace) .plist file for given environment to the main directory (it's src in my case):

    cp "${SRCROOT}/src/Resources/GoogleServiceInfoPlists/GoogleService-Info-$CONFIGURATION.plist" "${SRCROOT}/src/GoogleService-Info.plist"
    

${SRCROOT} - predefined, it points to your project location.

$CONFIGURATION - predefined, it's your build configuration, in my case: Debug, Stage, Prod. You can change this in Xcode: Project (not target!)->Info.

Please note that src/GoogleService-Info.plist file must be added to the Xcode project (Build Phases->Copy Bundle Resources) while /src/Resources/GoogleServiceInfoPlists/GoogleService-Info-* files not necessarily.

UPDATE:

Remember that your new Run Script must be placed before Copy Bundle Resources build phase. Otherwise, it won't work because it would be copied too late and the default version of the .plist file would be used.

查看更多
beautiful°
3楼-- · 2019-02-01 09:30
  1. Create a new folder (for example: GoogleServiceInfoPlists).

  2. Copy there all .plist files for each Configuration

for example:

GoogleService-Info-Debug.plist, 
GoogleService-Info-Alpha.plist,
GoogleService-Info-Beta.plist,
GoogleService-Info-Release.plist
  1. Add new Run Script Phase at last (Xcode: Target -> Build Phases -> "+" button).

  2. Use script below to copy .plist file for given environment to the build directory.

script:

RESOURCE_PATH=${SRCROOT}/${PRODUCT_NAME}/GoogleServiceInfoPlists/GoogleService-Info-$CONFIGURATION.plist

BUILD_APP_DIR=${BUILT_PRODUCTS_DIR}/${PRODUCT_NAME}.app

echo "Copying ${RESOURCE_PATH} to ${BUILD_APP_DIR}"
cp "${RESOURCE_PATH}" "${BUILD_APP_DIR}/GoogleService-Info.plist"

PS: You do not need to add the file to project. Just create a new folder in the main directory.

enter image description here

查看更多
SAY GOODBYE
4楼-- · 2019-02-01 09:33

I put two files with the (same) name GoogleService-Info.plist into my project.

enter image description here

One is at the root and one is in a folder called 'staging', so as to avoid a naming conflict in the file system.

Include the plist in only one project each

Including one in one target and the other in another makes it so that each target has a unique plist file with the correct name.

查看更多
登录 后发表回答