Simulate iPad using Cordova/PhoneGap emulator command

10,254

Solution 1

It may be that you were using an old version of phonegap/cordova, but in version 3.4 the following works for me:

cordova emulate ios --target="iPad" 

Solution 2

The Cordova emulate script is just a wrapper for the ios-sim command which you can use directly from the command line. Assuming your current working directory is the one with the emulate script in it, you can launch your build in the emulator in iPad mode with the following:

ios-sim launch ../build/myApp.app --family ipad --stderr console.log --stdout console.log &

The following is undoubtedly naive (I don't know shell-scripting), but I've hacked the emulate script to support a second command-line parameter which allows me to specify the device family. You might not be aware that the script already accepts a first parameter which allows you to specify the path to your project's .app file as it figures out the path if the parameter is not specified.

Update your version of the script with the following (GitHub fork here):

#! /bin/sh
#
# Licensing info removed for brevity
#

set -e

XCODE_VER=$(xcodebuild -version | head -n 1 | sed -e 's/Xcode //')
XCODE_MIN_VERSION="4.5"

if [[ "$XCODE_VER" < "$XCODE_MIN_VERSION" ]]; then
    echo "Cordova can only run in Xcode version $XCODE_MIN_VERSION or greater."
    exit 1
fi

CORDOVA_PATH=$( cd "$( dirname "$0" )" && pwd -P)
PROJECT_PATH="$(dirname "$CORDOVA_PATH")"
XCODEPROJ=$( ls "$PROJECT_PATH" | grep .xcodeproj  )
PROJECT_NAME=$(basename "$XCODEPROJ" .xcodeproj)

APP_PATH=$1
DEVICE_FAMILY=$2

if [ $# -lt 1 ]; then
    APP_PATH="$PROJECT_PATH/build/$PROJECT_NAME.app"
    DEVICE_FAMILY=iphone
fi

if [ ! -d "$APP_PATH" ]; then
    echo "Project '$APP_PATH' is not built. Building."
    $CORDOVA_PATH/build || exit $?
fi

if [ ! -d "$APP_PATH" ]; then
    echo "$APP_PATH not found to emulate."
    exit 1
fi

# launch using ios-sim
if which ios-sim >/dev/null; then
    ios-sim launch "$APP_PATH" --family "$DEVICE_FAMILY" --stderr "$CORDOVA_PATH/console.log" --stdout "$CORDOVA_PATH/console.log" &
else
    echo -e '\033[31mError: ios-sim was not found. Please download, build and install version 1.4 or greater from https://github.com/phonegap/ios-sim into your path. Or "brew install ios-sim" using homebrew: http://mxcl.github.com/homebrew/\033[m'; exit 1;
fi

You can then run the script like this (assumes your present working directory is the cordova directory containing the scripts):

./emulate ../build/myApp.app ipad

If you're always going to test on the iPad and you prefer not to have to specify the path to your app every time, you could just hardcode your preferred device family into the script like so and launch the emulator as you have been doing previously:

ios-sim launch "$APP_PATH" --family ipad  --stderr "$CORDOVA_PATH/console.log" --stdout "$CORDOVA_PATH/console.log" & 
Share:
10,254
Oliver Moran
Author by

Oliver Moran

Updated on June 04, 2022

Comments

  • Oliver Moran
    Oliver Moran almost 2 years

    I would like to use the included ./emulator command with Cordova/PhoneGap to run my app in the iPad simulator from the command line.

    The basic instructions are here:

    I've installed the iOS simulator from here:

    The documentation says it supports simulating an iPad from the command line. However, it opens by default to iPhone and changing the device to "iPad" closes the app (and it is not installed on the home screen). I've searched but can't find documentation to launch to simulate an iPad.

    How do I run the Cordova ./emulator command to open to iPad?

  • Asaf
    Asaf almost 10 years
    is there a link to the doc?
  • StarQuake
    StarQuake almost 10 years
    This works, but do you know how to run with iPad retina? target="iPad Retina" and --target="iPad (Retina)" do not seem to work
  • jujule
    jujule over 9 years
    works here with cordova run ios --target="ipad (retina)" see github.com/apache/cordova-ios/blob/…
  • Andrei Cojea
    Andrei Cojea about 9 years
    to get all available images, see this answer here: stackoverflow.com/questions/22310526/…