如何从命令行重置 iOS 模拟器?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/5125243/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How can I reset the iOS Simulator from the command line?
提问by Cameron Brown
I need to reset the iPhone Simulator a lot, and haven't found a way to do it without using the mouse. It's a small thing, but I'm really sick of doing it and would love to have a way to do this using a keyboard shortcut.
我需要经常重置 iPhone 模拟器,而且还没有找到不使用鼠标的方法。这是一件小事,但我真的厌倦了这样做,并且很想有办法使用键盘快捷键来做到这一点。
Even better would be a way to reset it from the command line, so I could build a reset into a deploy script.
更好的方法是从命令行重置它,这样我就可以将重置构建到部署脚本中。
I am not very familiar with iOS or MacOS.
我对 iOS 或 MacOS 不是很熟悉。
回答by Kappe
Just run this in the terminal:
只需在终端中运行它:
xcrun simctl erase all
improvement suggested by @txulu, just kill the simulator before execute the clean:
@txulu 建议的改进,只需在执行清理之前杀死模拟器:
killall "Simulator" 2> /dev/null; xcrun simctl erase all
回答by Alpine
In Xcode 6, DO NOT JUST DELETE THE FOLDER FOR THE SIMULATOR! It WILL screw things up, and it WILL cause you a headache.
在 Xcode 6 中,不要只是删除模拟器的文件夹!它会把事情搞砸,它会让你头疼。
In Xcode 6, there's actually a tool to control the simulator from the command line.
在 Xcode 6 中,实际上有一个工具可以从命令行控制模拟器。
Make sure your command line settings are set to Xcode 6
确保您的命令行设置设置为 Xcode 6
xcrun simctl
In Xcode 6, each device has a GUID/UUID associated to it, to reset a specific device, you need the GUID for it.
在 Xcode 6 中,每个设备都有一个关联的 GUID/UUID,要重置特定设备,您需要它的 GUID。
The command
命令
xcrun simctl list
will show you all of the devices you have set up. The output will look like this:
将显示您设置的所有设备。输出将如下所示:
== Devices ==
-- iOS 7.0 --
iPhone 4s (F77DC0AE-6A6D-4D99-9936-F9DB07BBAA82) (Shutdown)
iPhone 5 (5B78FC0D-0034-4134-8B1F-19FD0EC9D581) (Shutdown)
iPhone 5s (569E5910-E32D-40E2-811F-D2E8F04EA4EF) (Shutdown)
iPad 2 (451DBBD8-A387-4E77-89BF-2B3CD45B4772) (Shutdown)
iPad Retina (2C58366B-5B60-4687-8031-6C67383D793F) (Shutdown)
iPad Air (50E03D3B-3456-4C49-85AD-60B3AFE4918B) (Shutdown)
-- iOS 7.1 --
-- iOS 8.0 --
iPhone 4s (27818821-A0BB-496E-A956-EF876FB514C2) (Shutdown)
iPhone 5 (6FBAA7E2-857C-432A-BD03-980D762DA9D2) (Shutdown)
iPhone 5s (7675C82B-DE49-45EB-A28D-1175376AEEE9) (Shutdown)
iPad 2 (836E7C89-B9D6-4CC5-86DE-B18BA8600E7B) (Shutdown)
iPad Retina (EFDD043D-2725-47DC-A3FF-C984F839A631) (Shutdown)
iPad Air (9079AD6C-E74D-4D5F-9A0F-4933498B852E) (Shutdown)
Resizable iPhone (943CFEDE-A03C-4298-93E3-40D0713652CB) (Shutdown)
Resizable iPad (DBA71CA5-6426-484B-8E9B-13FCB3B27DEB) (Shutdown)
Just copy the GUID from inside the parentheses, and run xcrun simctl erase
就在GUID从括号内复制,并运行 xcrun simctl erase
for example,
例如,
xcrun simctl erase 5B78FC0D-0034-4134-8B1F-19FD0EC9D581
would erase the iOS 7.0, iPhone 5 device
将擦除 iOS 7.0、iPhone 5 设备
回答by Cameron Brown
Thought I'd post this for anyone who runs into the same need. Someone on reddit gave me this solution (which I tested and it works great). Note that this time you need an ellipsis after "Settings", not three periods (weird).
以为我会为遇到相同需求的任何人发布此信息。reddit 上有人给了我这个解决方案(我测试过,效果很好)。请注意,这次您需要在“设置”之后使用省略号,而不是三个句点(奇怪)。
This is an AppleScript that can be invoked from the command line to reset the Simulator:
这是一个可以从命令行调用以重置模拟器的 AppleScript:
tell application "iPhone Simulator"
activate
end tell
tell application "System Events"
tell process "iPhone Simulator"
tell menu bar 1
tell menu bar item "iOs Simulator"
tell menu "iOs Simulator"
click menu item "Reset Content and Settings…"
end tell
end tell
end tell
tell window 1
click button "Reset"
end tell
end tell
end tell
Save as /path/to/script
and invoke with:
另存为/path/to/script
并调用:
osascript /path/to/script
回答by Joe Susnick
COPY-PASTE ANSWER - note: will reset the contents and settings of all available simulators.
复制粘贴答案 - 注意:将重置所有可用模拟器的内容和设置。
Thanks @Alpine for the inspiration and knowledge. If you run this in your command line you should be able to reset all the available sims. This works with Xcode 6.
感谢@Alpine 的灵感和知识。如果您在命令行中运行它,您应该能够重置所有可用的 sims。这适用于 Xcode 6。
# Get the sim list with the UUIDs
OUTPUT="$(xcrun simctl list)"
# Parse out the UUIDs and saves them to file
echo $OUTPUT | awk -F "[()]" '{ for (i=2; i<NF; i+=2) print $i }' | grep '^[-A-Z0-9]*$' > output.txt
# Iterate through file and reset sim
for UUID in `awk '{ print }' output.txt`
do
xcrun simctl erase $UUID
done
回答by LGFox
I checked it with XCode 9. To close all active simulators run:
我用 XCode 9 检查过它。要关闭所有活动的模拟器,请运行:
xcrun simctl shutdown all
To reset all simulators run:
要重置所有模拟器,请运行:
xcrun simctl erase all
You can filter what simulator to close/reset like this:
您可以像这样过滤要关闭/重置的模拟器:
xcrun simctl shutdown F36B238F-3ED6-4E10-BB5A-0726151916FA
xcrun simctl erase F36B238F-3ED6-4E10-BB5A-0726151916FA
Find all accessible simulators (and their GUID) on your machine like this:
在您的机器上查找所有可访问的模拟器(及其 GUID),如下所示:
xcrun instruments -s
To run any simulator by GUID:
要通过 GUID 运行任何模拟器:
xcrun instruments -w F36B238F-3ED6-4E10-BB5A-0726151916FA -t Blank
To install app to the booted simulator:
要将应用程序安装到启动的模拟器:
xcrun simctl install booted /path/to/your.app
To remove app from the booted simulator:
从启动的模拟器中删除应用程序:
xcrun simctl uninstall booted /path/to/your.app
To launch the app in the booted simulator:
在启动的模拟器中启动应用程序:
xcrun simctl launch booted "com.app.bundleIdentifier"
"com.app.bundleIdentifier" is your CFBundleIdentifier in Info.plist
“com.app.bundleIdentifier”是Info.plist中的CFBundleIdentifier
回答by jdc
Delete the contents of
删除内容
~/Library/Application Support/iPhone Simulator/<sdk revision>
And you're good to go.
你很高兴去。
回答by pkamb
Upon installing Xcode I always create a keyboard shortcut for "Reset Content and Settings" in the simulator. An extremely useful time saver.
安装 Xcode 后,我总是在模拟器中为“重置内容和设置”创建一个键盘快捷键。一个非常有用的节省时间。
System Preferences > Keyboard > Shortcuts > App Shortcuts > "+"
System Preferences > Keyboard > Shortcuts > App Shortcuts > "+"
In the Application picker, select "Other..." to open the app picker dialog.
在应用程序选择器中,选择“其他...”以打开应用程序选择器对话框。
In this dialog you're unable to "Show Package Contents" to explore a .app, so you'll need to use Go to Folder
via Cmd-Shift-G. (First open the application drop down and select Other
)
在此对话框中,您无法“显示包内容”来浏览 .app,因此您需要使用Go to Folder
via Cmd- Shift- G。(首先打开应用程序下拉菜单并选择Other
)
In the current version of Xcode, go to the path:
在Xcode的最新版本,请在路径:
/Applications/Xcode/Contents/Developer/Applications
/Applications/Xcode/Contents/Developer/Applications
Select Simulator.app
and press "Add"
选择Simulator.app
并按“添加”
For Menu Title
, enter Reset Content and Settings...
对于Menu Title
,输入Reset Content and Settings...
For Keyboard Shortcut
, press CMD-Shift-R
对于Keyboard Shortcut
,请按CMD- Shift-R
回答by Oded Regev
The keyboard short-cut solution is not relevant anymore and unfortunately @Cameron solution didn't work for me either (I tried to debug it with no luck)
键盘快捷键解决方案不再相关,不幸的是@Cameron 解决方案对我也不起作用(我试图调试它但没有运气)
Here is what works for me:
以下是对我有用的方法:
#!/bin/bash
# `menu_click`, by Jacob Rus, September 2006
#
# Accepts a list of form: `{"Finder", "View", "Arrange By", "Date"}`
# Execute the specified menu item. In this case, assuming the Finder
# is the active application, arranging the frontmost folder by date.
osascript <<SCRIPT
on menu_click(mList)
local appName, topMenu, r
-- Validate our input
if mList's length < 3 then error "Menu list is not long enough"
-- Set these variables for clarity and brevity later on
set {appName, topMenu} to (items 1 through 2 of mList)
set r to (items 3 through (mList's length) of mList)
-- This overly-long line calls the menu_recurse function with
-- two arguments: r, and a reference to the top-level menu
tell application "System Events" to my menu_click_recurse(r, ((process appName)'s ?
(menu bar 1)'s (menu bar item topMenu)'s (menu topMenu)))
end menu_click
on menu_click_recurse(mList, parentObject)
local f, r
-- `f` = first item, `r` = rest of items
set f to item 1 of mList
if mList's length > 1 then set r to (items 2 through (mList's length) of mList)
-- either actually click the menu item, or recurse again
tell application "System Events"
if mList's length is 1 then
click parentObject's menu item f
else
my menu_click_recurse(r, (parentObject's (menu item f)'s (menu f)))
end if
end tell
end menu_click_recurse
application "iPhone Simulator" activate
menu_click({"iPhone Simulator", "iOS Simulator", "Reset Content and Settings…"})
tell application "System Events"
tell process "iPhone Simulator"
tell window 1
click button "Reset"
end tell
end tell
end tell
SCRIPT
回答by mpatzer
I wrote a script that will reset the contents & settings of all versions and devices for the iOS Simulator. It grabs the device names and version numbers from the menu, so it will include any new devices or iOS versions that Apple releases simulators for.
我编写了一个脚本,可以为 iOS 模拟器重置所有版本和设备的内容和设置。它从菜单中获取设备名称和版本号,因此它将包含 Apple 为其发布模拟器的任何新设备或 iOS 版本。
It's easy to run manually or use in a build-script. I would suggest adding it as a Pre-Action Run Script before the build.
手动运行或在构建脚本中使用很容易。我建议在构建之前将其添加为 Pre-Action Run Script。
It's based heavily on Stian's script above, but doesn't become stale with new iOS versions, and eliminates the dialog box (better for automation build scripts and working from the command-line).
它主要基于上面 Stian 的脚本,但不会随着新的 iOS 版本变得陈旧,并且消除了对话框(更适合自动化构建脚本和从命令行工作)。
回答by Sjakelien
I have found this very helpful tool called "SimulatorManager": http://tue-savvy.github.ioIt will reset all you simulators by means of a menu bar widget (not sure if that's what its called) but on top of that it will give you quick access to all your application data. I really can't live without it anymore. Spread the word!
我发现了这个名为“SimulatorManager”的非常有用的工具:http: //tue-savvy.github.io它将通过菜单栏小部件(不确定这是否是它的名字)重置所有模拟器,但最重要的是它将让您快速访问所有应用程序数据。我真的不能没有它了。传播这个词!