Running Your iOS App in the Simulator From The Command Line

本文介绍如何通过命令行在iOS模拟器中启动应用,并记录模拟器操作视频。使用WaxSim工具可以轻松实现这一过程。

While Apple doesn’t provide an “official” way to launch an app in the iOS Simulator from the command line, there’s a few private tricks we can employ.First, the iOS Simulator app itself is located in:

/Developer/Platforms/iPhoneSimulator.platform/Developer/Applications

But running that will only bring up the simulator in the last state it was in. How do we install an application in there and then run it automatically like Xcode does?

The trick is in the private iPhoneSimulatorRemoteClient.framework. Now before you freak out, don’t worry! This isn’t something you compile your app against and submit to the store. This is a framework that we can use on our local machines to control the simulator just like Xcode does. The best part is, all the work of hooking into it has been done for us!

There are two command line utilities I’ve seen that do this best. I used to useiphonesim and it works great. But I recently switched to WaxSim since it also records video of the simulator in motion. They both are functionally equivalent and small enough to understand. It’s your call, but I recommend the later.1

The Recipe 

First, we need to pull down the WaxSim source and install it. Run the following:

git clone https://github.com/square/WaxSim
cd WaxSim
xcodebuild install DSTROOT=/

Now, you have the waxsim binary in /usr/local/bin which should already be on your path. You can now invoke your application in the simulator like this:

waxsim [application path]

So, where is your application path? That depends on how you build it! First, we’ll need to do a quick walkthrough about building Xcode projects from the command line.

cd into your iOS application’s project directory (the one with the Xcode project file). Then, invoke the xcodebuild command like so:

xcodebuild -sdk iphonesimulator5.0 \
           -arch i386 \
           install DSTROOT=/tmp/MyApp

For most projects, this command will work just fine. However, if you have an explicit workspace set up, you’ll need to use man xcodebuild to learn how to teach the xcodebuild binary to interpret your setup. There’s a lot of good stuff in there about how to specify whether to build for the simulator or device, too.

The DSTROOT environment variable on the command above is the most important part. DSTROOT is used by Xcode when run with the “install” build action to figure out where to put the results. We’re specifying /tmp/MyApp explicitly so that we know where the resulting application bundle will be to hand in to waxsim.

Once the xcodebuild command completes, you should now have an *.app bundle in /tmp/MyApp/Applications/[YourAppName].app. Of course, subtitute your own application name in the path. Invoke waxsim and you’re up and running:

waxsim /tmp/MyApp/Applications/[YourAppName].app

This runs the iPhone simulator with the latest SDK that waxsim is aware of. Usewaxsim -h to find out other options like running the iPad simulator or other using other available SDKs.

You can also record a low resolution video of the simulator like so

waxsim -v myvideo.mov /tmp/MyApp/Applications/[YourAppName].app

And now you’ll have myvideo.mov in the current directory. It’s a pretty low res and stuttery video, probably not something you’d use for a promotional demo, but it’s a nice feature if you need to review results of automated testing workflows.

Script It 

So, for completeness sake, here’s a shell script that you can modify to build and run your application:

#!/bin/bash

# Tell bash to abort the script if any error happens
set -e

APPNAME=MyApp
DSTROOT=/tmp/MyApp

xcodebuild -sdk iphonesimulator5.0 \
           -arch i386 \
           install DSTROOT="$DSTROOT"

waxsim "$DSTROOT"/Applications/"$APPNAME".app

Drop this in your project directory and tweak to taste.

The End!

So, there you have it. We’ve built the waxsim binary, learned briefly how to build with xcodebuild, and learned how to launch your application in the iOS simulator on demand and from the command line. Vive la Unix!

ios@Mac-mini-2 ~ % appium-doctor WARN AppiumDoctor [Deprecated] Please use appium-doctor installed with "npm install @appium/doctor --location=global" info AppiumDoctor Appium Doctor v.1.16.2 info AppiumDoctor ### Diagnostic for necessary dependencies starting ### info AppiumDoctor ✔ The Node.js binary was found at: /usr/local/bin/node info AppiumDoctor ✔ Node version is 22.20.0 info AppiumDoctor ✔ Xcode is installed at: /Users/ios/Desktop/Xcode16.app/Contents/Developer info AppiumDoctor ✔ Xcode Command Line Tools are installed in: /Users/ios/Desktop/Xcode16.app/Contents/Developer info AppiumDoctor ✔ DevToolsSecurity is enabled. info AppiumDoctor ✔ The Authorization DB is set up properly. info AppiumDoctor ✔ Carthage was found at: /opt/homebrew/bin/carthage. Installed version is: 0.40.0 info AppiumDoctor ✔ HOME is set to: /Users/ios info AppiumDoctor ✔ ANDROID_HOME is set to: /Users/ios/Library/Android/sdk info AppiumDoctor ✔ JAVA_HOME is set to: /Library/Java/JavaVirtualMachines/jdk-21.jdk/Contents/Home info AppiumDoctor Checking adb, android, emulator, apkanalyzer info AppiumDoctor 'adb' is in /Users/ios/Library/Android/sdk/platform-tools/adb info AppiumDoctor 'emulator' is in /Users/ios/Library/Android/sdk/emulator/emulator WARN AppiumDoctor ✖ android, apkanalyzer could NOT be found in /Users/ios/Library/Android/sdk! info AppiumDoctor ✔ 'bin' subfolder exists under '/Library/Java/JavaVirtualMachines/jdk-21.jdk/Contents/Home' info AppiumDoctor ### Diagnostic for necessary dependencies completed, one fix needed. ### info AppiumDoctor info AppiumDoctor ### Diagnostic for optional dependencies starting ### WARN AppiumDoctor ✖ opencv4nodejs cannot be found. WARN AppiumDoctor ✖ ffmpeg cannot be found WARN AppiumDoctor ✖ mjpeg-consumer cannot be found. WARN AppiumDoctor ✖ set-simulator-location is not installed WARN AppiumDoctor ✖ idb and idb_companion are not installed WARN AppiumDoctor ✖ applesimutils cannot be found info AppiumDoctor ✔ ios-deploy is installed at: /usr/local/bin/ios-deploy. Installed version is: 1.12.2 WARN AppiumDoctor ✖ bundletool.jar cannot be found WARN AppiumDoctor ✖ gst-launch-1.0 and/or gst-inspect-1.0 cannot be found info AppiumDoctor ### Diagnostic for optional dependencies completed, 8 fixes possible. ### info AppiumDoctor info AppiumDoctor ### Manual Fixes Needed ### info AppiumDoctor The configuration cannot be automatically fixed, please do the following first: WARN AppiumDoctor ➜ Manually install android, apkanalyzer and add it to PATH. https://developer.android.com/studio#cmdline-tools and https://developer.android.com/studio/intro/update#sdk-manager may help to setup. info AppiumDoctor info AppiumDoctor ### Optional Manual Fixes ### info AppiumDoctor The configuration can install optionally. Please do the following manually: WARN AppiumDoctor ➜ Why opencv4nodejs is needed and how to install it: http://appium.io/docs/en/writing-running-appium/image-comparison/ WARN AppiumDoctor ➜ ffmpeg is needed to record screen features. Please read https://www.ffmpeg.org/ to install it WARN AppiumDoctor ➜ mjpeg-consumer module is required to use MJPEG-over-HTTP features. Please install it with 'npm i -g mjpeg-consumer'. WARN AppiumDoctor ➜ set-simulator-location is needed to set location for Simulator. Please read https://github.com/lyft/set-simulator-location to install it WARN AppiumDoctor ➜ Why idb is needed and how to install it: https://github.com/appium/appium-idb WARN AppiumDoctor ➜ Why applesimutils is needed and how to install it: http://appium.io/docs/en/drivers/ios-xcuitest/ WARN AppiumDoctor ➜ bundletool.jar is used to handle Android App Bundle. Please read http://appium.io/docs/en/writing-running-appium/android/android-appbundle/ to install it WARN AppiumDoctor ➜ gst-launch-1.0 and gst-inspect-1.0 are used to stream the screen of the device under test. Please read https://appium.io/docs/en/writing-running-appium/android/android-screen-streaming/ to install them and for more details info AppiumDoctor info AppiumDoctor ### info AppiumDoctor info AppiumDoctor Bye! Run appium-doctor again when all manual fixes have been applied! info AppiumDoctor 所以要加入环境的是appium-doctor显示的这些环境变量位置吗
12-31
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值