Search code examples
iosapp-transport-security

How do you change App Transport Security Settings based on the build configuration?


I have an app with multiple build configurations. If the selected configuration is Debug, I want NSAllowsArbitraryLoads key in the Info.plist file to be set as YES, else I want it to be set as NO.

How do I go about achieving this?


Solution

  • The solution I found requires the use of PlistBuddy:

    In your project settings, select Build Phase > click + to add a new run script build phase.

    Name the phase "App Transport Security". Paste the following script:

    if [ "${CONFIGURATION}" = "Release" ]; 
    then 
        /usr/libexec/PlistBuddy -c "Set :NSAppTransportSecurity:NSAllowsArbitraryLoads false" "${TARGET_BUILD_DIR}/${INFOPLIST_PATH}" 
    else 
        /usr/libexec/PlistBuddy -c "Set :NSAppTransportSecurity:NSAllowsArbitraryLoads true" "${TARGET_BUILD_DIR}/${INFOPLIST_PATH}" 
    fi