Search code examples
iosxcodeshellplistinfo-plist

How to enable 3D touch(static quick actions) only in debug mode?


Our team is developing an app, and I'd like to add some home screen quick actions just for debug purposes. Also, I want it to be enabled immediately after a fresh install, which means dynamic quick actions would not be an option. However, I have no idea if we can enable static quick actions only in debug mode. Is there any way to achieve this?


Solution

  • You have two major options for this:

    - The GENERAL option for any kind of file:

    The cleanest way is to have separate files for each configuration. Then:

    • You can set the path for each configuration in project build settings like this:

    Project build settings

    • Or you can use run script for this or any file you need to change during the build process:

      • Create two different info.plist files, one for the debug and another for production
      • Head to the project build settings and create new run script phase
      • Use the following script:
    sourceFilePath="$PROJECT_DIR/$PROJECT_NAME/"
    debugFileName="Debug-Info.plist"
    releaseFileName="Release-Info.plist"
    
    if [ "$CONFIGURATION" == "Debug" ]; then
    cp $sourceFilePath/$debugFileName "$INFOPLIST_FILE"
    else
    cp $sourceFilePath/$releaseFileName "$INFOPLIST_FILE"
    fi
    

    Note that in this example:

    • I use Debug-Info.plist for debug mode file.
    • I use Release-Info.plist for release mode file.
    • I copied all files in same directory as the original info.plist file.

    But I made all variables and you can change them to whatever you want.

    - The More SPECIFIC option for any plist file:

    Since Info.plist is a property list, you can use PlistBuddy to edit any value of of it directly. Here is the example script to add a shortcut item if it is in debug mode only:

    /usr/libexec/PlistBuddy -c "Delete :UIApplicationShortcutItems" "$INFOPLIST_FILE"
    
    if [ "$CONFIGURATION" != "Debug" ]; then
    exit
    fi
    
    /usr/libexec/PlistBuddy -c "add :UIApplicationShortcutItems array" "$INFOPLIST_FILE"
    /usr/libexec/PlistBuddy -c "delete :UIApplicationShortcutItems" "$INFOPLIST_FILE"
    /usr/libexec/PlistBuddy -c "add :UIApplicationShortcutItems array" "$INFOPLIST_FILE"
    /usr/libexec/PlistBuddy -c "add :UIApplicationShortcutItems:0 dict" "$INFOPLIST_FILE"
    /usr/libexec/PlistBuddy -c "add :UIApplicationShortcutItems:0:UIApplicationShortcutItemIconType string UIApplicationShortcutIconTypePlay" "$INFOPLIST_FILE"
    /usr/libexec/PlistBuddy -c "add :UIApplicationShortcutItems:0:UIApplicationShortcutItemTitle string Play" "$INFOPLIST_FILE"
    /usr/libexec/PlistBuddy -c "add :UIApplicationShortcutItems:0:UIApplicationShortcutItemSubtitle string Start playback" "$INFOPLIST_FILE"
    /usr/libexec/PlistBuddy -c "add :UIApplicationShortcutItems:0:UIApplicationShortcutItemType string PlayMusic" "$INFOPLIST_FILE"
    /usr/libexec/PlistBuddy -c "add :UIApplicationShortcutItems:0:UIApplicationShortcutItemUserInfo dict" "$INFOPLIST_FILE"
    /usr/libexec/PlistBuddy -c "add :UIApplicationShortcutItems:0:UIApplicationShortcutItemUserInfo:firstShortcutKey1 string firstShortcutKeyValue1" "$INFOPLIST_FILE"
    

    Remember to run this script sometime before Copy Bundle Resources.

    Run scripts sequence

    I recommend you to always put script codes inside a separate file and call just call it in the build phase.