Ios – Use same CFBundleVersion and CFBundleShortVersionString in all targets

iosversionxcode

I received the following email from Apple when I submit an app update:

We have discovered one or more issues with your recent delivery for
"Project". Your delivery was successful, but you may
wish to correct the following issues in your next delivery:

CFBundleVersion Mismatch – The CFBundleVersion value '1' of extension
'Project.app/PlugIns/ProjectTodayExtension.appex' does not
match the CFBundleVersion value '985' of its containing iOS
application 'Project.app'.

CFBundleShortVersionString Mismatch – The CFBundleShortVersionString
value '1.0' of extension 'Project.app/PlugIns/ProjectTodayExtension.appex' does not
match the CFBundleShortVersionString value '2.1.6' of its containing
iOS application 'Project.app'.

After you’ve corrected the issues, you can use Xcode or Application
Loader to upload a new binary to iTunes Connect.

Is there any way of use the same CFBundleVersion and CFBundleShortVersionString in all targets to prevent this?

Best Solution

My solution is:

For CFBundleShortVersionString:

  • Add a user-defined constant in your project settings

Add a user-defined constant in your **project** settings

  • Name it $(CF_BUNDLE_SHORT_VERSION_STRING) and set it to your desired value

enter image description here

  • Set your version in your targets to $(CF_BUNDLE_SHORT_VERSION_STRING)

enter image description here

  • Repeat for all targets. Done!

CFBundleVersion: you could do the same for CFBundleVersion, but somehow I wanted this value to be computed from my GIT repo commit count. I´ve done it like this:

  • Add a Pre-action to your main target. You access the shown dialog via Product > Scheme > Edit Scheme

enter image description here

  • Add a Post-action to your main target.

enter image description here

  • Add a new Command Line Tool target named BundleVersionUpdate and one named BundleVersionRevert

enter image description here

  • Navigate to your new BundleVersionUpdate target and add a new Run Script Build Phase

enter image description here

  • Paste the following
\#!/bin/sh

INFOPLIST="${SRCROOT}/MyApp/MyApp-Info.plist"
INFOPLIST_WKAPP="${SRCROOT}/MyApp-WKApp/Info.plist"
INFOPLIST_WKEXT="${SRCROOT}/MyApp-WKExt/Info.plist"

PLISTCMD="Set :CFBundleVersion $(git rev-list --all|wc -l)"

echo -n "$INFOPLIST" 
| xargs -0 /usr/libexec/PlistBuddy -c "$PLISTCMD"

echo -n "$INFOPLIST_WKAPP" 
| xargs -0 /usr/libexec/PlistBuddy -c "$PLISTCMD"

echo -n "$INFOPLIST_WKEXT" 
| xargs -0 /usr/libexec/PlistBuddy -c "$PLISTCMD"
  • Navigate to your new BundleVersionRevert target and add a new Run Script Build Phase and paste this:
\#!/bin/sh

INFOPLIST="${SRCROOT}/MyApp/MyApp-Info.plist"
INFOPLIST_WKAPP="${SRCROOT}/MyApp-WKApp/Info.plist"
INFOPLIST_WKEXT="${SRCROOT}/MyApp-WKExt/Info.plist"

PLISTCMD="Set :CFBundleVersion SCRIPTED"

echo -n "$INFOPLIST" 
| xargs -0 /usr/libexec/PlistBuddy -c "$PLISTCMD"

echo -n "$INFOPLIST_WKAPP" 
| xargs -0 /usr/libexec/PlistBuddy -c "$PLISTCMD"

echo -n "$INFOPLIST_WKEXT" 
| xargs -0 /usr/libexec/PlistBuddy -c "$PLISTCMD"
  • Enjoy!