ionic/cordova emulate vs. Xcode 7

After the App Store automatically updated Xcode to version 7 (without me actually wanting this for now, but hey …) running ionic emulate ios -c -l would not do much anymore:

  • The app would launch but get stuck at the loading screen
  • No console.log() calls would be logged

Fixing it manually

To fix this, update the platforms/ios/projectname/projectname-Info.plist file with the following settings:

<key>NSAppTransportSecurity</key>
<dict>
  <key>NSAllowsArbitraryLoads</key>
  <true/>
</dict>

Add the snippet above at the end of the file, right before the closing </dict></plist>

Do note that you’ll have to (re)do this every time you run ionic platform add ios (which can happen when working in a team with multiple people)

Fixing it automatically

Alternatively you can use a before_emulate hook to have this change applied automatically before starting the iPhone Simulator.

  1. Put the contents below in ./hooks/before_emulate/010_ios_allowarbitraryloads.sh
  2. Make the script executable (chmod 755 ./hooks/before_emulate/010_ios_allowarbitraryloads.sh)

Every time you (or one of your teammates) runs ionic emulate ios it will check if the change in platforms/ios/projectname/projectname-Info.plist has already been made or not. If not, the settings are added.

#!/bin/bash

# Allow iPhone Simulator to make Arbitrary Loads
# @ref https://www.bram.us/2015/09/29/ionic-emulate-vs-xcode-7/

XCODEVERSION=`xcodebuild -version | grep Xcode | sed 's/Xcode //g'`
XCODEMAINVERSION=`echo $XCODEVERSION | cut -d "." -f 1`
PROJECTNAME=`xmllint --format --xpath "//*[local-name()='widget']/*[local-name()='name'][1]/text()" config.xml`

if [[ "$CORDOVA_PLATFORMS" == "ios" ]]
then

	if [[ "$XCODEMAINVERSION" > 6 ]]
	then

		echo "iPhone Simulator (XCode $XCODEVERSION) is being used. We might need to adjust $PROJECTNAME-Info.plist to allow Arbitrary Loads!"

		PLISTBUDDY="/usr/libexec/PlistBuddy"
		TARGET="platforms/ios/$PROJECTNAME/$PROJECTNAME-Info.plist"
		HASSETTING=`$PLISTBUDDY -c "print :NSAppTransportSecurity:NSAllowsArbitraryLoads" "$TARGET" 2>&1`

		if [[ "$HASSETTING" == "true" ]]
		then

			echo " - NSAllowsArbitraryLoads already enabled. Not adjusting $PROJECTNAME-Info.plist"

		else

			echo " - NSAllowsArbitraryLoads not enabled. Adjusting $PROJECTNAME-Info.plist"

			$PLISTBUDDY -c "Add :NSAppTransportSecurity dict" "$TARGET"
			$PLISTBUDDY -c "Add :NSAppTransportSecurity:NSAllowsArbitraryLoads bool YES" "$TARGET"

		fi

	fi

fi
Did this help you out? Like what you see?
Consider donating.

I don’t run ads on my blog nor do I do this for profit. A donation however would always put a smile on my face though. Thanks!

☕️ Buy me a Coffee ($3)

Published by Bramus!

Bramus is a frontend web developer from Belgium, working as a Chrome Developer Relations Engineer at Google. From the moment he discovered view-source at the age of 14 (way back in 1997), he fell in love with the web and has been tinkering with it ever since (more …)

Unless noted otherwise, the contents of this post are licensed under the Creative Commons Attribution 4.0 License and code samples are licensed under the MIT License

Join the Conversation

1 Comment

  1. This fixed my issues. I just had to remove ios, remove the platform dir and then add ios like so:
    ionic platform rm ios
    rm -rf ./platforms/ios
    ionic platform add ios
    ionic prepare ios
    ionic emulate ios -c -l

Leave a comment

Leave a Reply to Angel S. Moreno Cancel reply

Your email address will not be published.

This site uses Akismet to reduce spam. Learn how your comment data is processed.