Simulate iPad using Cordova/PhoneGap emulator command

醉酒当歌 提交于 2019-12-10 12:46:45

问题


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:

  • http://docs.phonegap.com/en/2.2.0/guide_command-line_index.md.html

I've installed the iOS simulator from here:

  • https://github.com/phonegap/ios-sim

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?


回答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" 



回答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" & 



回答3:


For me all the mentioned options here did not work, I had to call it with this command to show the iPad Retina:

``ios-sim launch [DIR_OF_APP]platforms/ios/build/emulator/My-App.app --devicetypeid "com.apple.CoreSimulator.SimDeviceType.iPad-Retina, 8.2"

To retrieve all the devicetypeid's type ios-sim showdevicetypes



来源:https://stackoverflow.com/questions/13877840/simulate-ipad-using-cordova-phonegap-emulator-command

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!