如何从命令行重置iOS模拟器?

我需要重置iPhone模拟器很多,并没有find一种方法来做到这一点,而不使用鼠标。 这是一件小事,但我真的很难这样做,并希望有一种方法来使用键盘快捷方式做到这一点。

更好的办法是从命令行重置它,所以我可以build立一个重置到部署脚本。

我对iOS或MacOS不是很熟悉。

在Xcode 6中,不要只删除模拟器的文件夹! 它会把事情搞砸,这会让你头痛。

在Xcode 6中,实际上有一个从命令行控制模拟器的工具。

确保您的命令行设置被设置为Xcode 6

 xcrun simctl 

在Xcode 6中,每个设备都有一个与之关联的GUID / UUID,为了重置一个特定的设备,你需要GUID。

命令

 xcrun simctl list 

将向您展示您设置的所有设备。 输出结果如下所示:

 == 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) 

只需从圆括号中复制GUID,然后运行xcrun simctl erase

例如,

 xcrun simctl erase 5B78FC0D-0034-4134-8B1F-19FD0EC9D581 

会清除iOS 7.0,iPhone 5设备

简单:

 xcrun simctl erase all 

@txulubuild议的改进,在执行clean之前杀死模拟器:

 killall "Simulator" 2> /dev/null; xcrun simctl erase all 

以为我会张贴这个谁遇到同样的需要的人。 有人在reddit给了我这个解决scheme(我testing,它工作很好)。 请注意,这次你需要在“设置”后面有一个省略号,而不是三个周期(怪异的)。

这是一个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 

另存为/path/to/script并调用:

 osascript /path/to/script 

复制粘贴回答 – 注意:将重置所有可用模拟器的内容和设置。

感谢@Alpine的灵感和知识。 如果你在你的命令行中运行它,你应该能够重置所有可用的模拟器。 这适用于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 $1 }' output.txt` do xcrun simctl erase $UUID done 

删除的内容

 ~/Library/Application Support/iPhone Simulator/<sdk revision> 

你很好走。

键盘快捷方式不再相关,不幸的@Cameron解决scheme也不适用于我(我试图debugging它没有运气)

这是对我有用的东西:

 #!/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 

在安装Xcode时,我总是在模拟器中创build一个“重置内容和设置”的键盘快捷键。 一个非常有用的节省时间。

System Preferences > Keyboard > Shortcuts > App Shortcuts > "+"

在应用程序select器中,select“其他…”以打开应用程序select器对话框。

在这个对话框中,你无法“显示包内容”来浏览一个.app,所以你需要通过CmdShiftG来使用Go to Folder 。 (首先打开应用程序下拉菜单并selectOther

在当前版本的Xcode中,转到path:

/Applications/Xcode/Contents/Developer/Applications

selectSimulator.app并点击“添加”

对于Menu Title ,请inputReset Content and Settings...

对于Keyboard Shortcut ,请按CMDShiftR

重置内容和设置

我写了一个脚本,将重置iOS模拟器的所有版本和设备的内容和设置。 它从菜单中抓取设备名称和版本号,所以它将包括Apple发布模拟器所用的任何新设备或iOS版本。

手动运行或在构build脚本中使用很容易。 我build议在构build之前将其添加为Pre-Action Run Script。

它很大程度上依赖于上面的Stian的脚本,但是不会因为新的iOS版本而过时,并且会消除对话框(更适合自动化构build脚本和从命令行工作)。

https://github.com/michaelpatzer/ResetAllSimulators

我发现这个非常有用的工具叫做“SimulatorManager”: http : //tue-savvy.github.io它会通过一个菜单栏小部件(不知道它是否叫做)来重置所有的模拟器,但是最重要的是它会让您快速访问所有的应用程序数据。 我真的活不下去了。 传播这个词!

我想添加一些东西给卡梅隆布朗的答案 。 为了确保正确的版本被重置(例如,iPad,版本6.1),我通过ios-sim启动iOS模拟器:

 version=$(echo "$DESTINATION" | egrep -o "OS=[0-9.]{3}" | cut -d '=' -f 2) simType=$(echo "$DESTINATION" | egrep -o "name=[a-zA-Z]*" | cut -d '=' -f 2 | tr "[AZ]" "[az]") IOS_SIM_BIN=$(which ios-sim) if [ -z $IOS_SIM_BIN ] then echo "ios-sim not installed, please use 'sudo npm install ios-sim -g'" fi echo "Resetting Simulator \"$simType\", version \"$version\"" $IOS_SIM_BIN start --family $simType --sdk $version --timeout 1 osascript /path/to/reset_simulator.applescript 

$DESTINATION可以是例如"OS=7.0,name=iPad"

为了正常工作,我调整了一下reset_simulator.applescript并删除了激活部分:

 tell application "iPhone Simulator" activate end tell 

作为使用xcrun命令的额外好处,您可以在列出之后启动设备

 xcrun simctl list 

一旦你有清单显示运行:

 xcrun instruments -w "iPhone 5s (8.1 Simulator) [31E5EF01-084B-471B-8AC6-C1EA3167DA9E]" 

我们使用下面的python脚本来重置构build服务器上的模拟器。

 #!/usr/bin/env python import subprocess import re import os def uninstall_app(): print 'Running %s' % __file__ # Get (maybe read from argv) your bundle identifier eg com.mysite.app_name try: bundle_identifier = '$' + os.environ['BUNDLE_IDENTIFIER'] except KeyError, e: print 'Environment variable %s not found. ' % e print 'Environment: ', os.environ exit(1) print 'Uninstalling app with Bundle identifier: ', bundle_identifier # We call xcrun, and strip the device GUIDs from the output process = subprocess.Popen(['xcrun', 'simctl', 'list'], stdout=subprocess.PIPE) # Read first line line = process.stdout.readline() while True: # Assume first match inside parenthesis is what we want m = re.search('\((.*?)\)', line) if not (m is None): # The regex found something, # group(1) will throw away the surrounding parenthesis device_GUID = m.group(1) # Brutely call uninstall on all listed devices. We know some of these will fail and output an error, but, well.. subprocess.call(['xcrun', 'simctl', 'uninstall', device_GUID, bundle_identifier]) # Read next line line = process.stdout.readline() # Stop condition if line == '': break if __name__ == '__main__': uninstall_app() 

它假设你的应用程序的包标识符被设置为一个环境variables,例如

 export BUNDLE_IDENTIFIER=com.example.app_name 

也许你想以另一种方式传递包标识符。

基于上面的大部分答案,我正在使用键盘大师,并做了一个小macros来重置当前运行的模拟器,并重新启动它。 在重置和重新启动后,它将焦点转回到Xcode,所以我可以立即再次点击Command+R来重新运行应用程序,我觉得这很方便。

在这里输入图像描述

ruby脚本的内容是:

 #!/usr/bin/env ruby list = `xcrun simctl list`.split("\n") list.each do |line| if line =~ /\(Booted\)$/ device = line.match(/([^(]*)\s+\(([^)]*)\)\s+\(([^)]*)\).*/)[1] uuid = line.match(/([^(]*)\s+\(([^)]*)\)\s+\(([^)]*)\).*/)[2] status = line.match(/([^(]*)\s+\(([^)]*)\)\s+\(([^)]*)\).*/)[3] puts uuid break end end 

这里有一个Rakefile任务来重置一个目标模拟器。 由于Xcode 7命令行工具破坏了xcrun simctl uninstall命令,所以它可以在Xcode 7中使用。 我有一个自定义的runC方法,因为我喜欢看到实际的terminal命令以及它的输出。

 desc "Resets the iPhone Simulator state" task :reset_simulator => [] do deviceDestinationName = 'iPhone 6' #for efficiency since we only target one device for our unit tests puts "...starting simulator reset" runC('killall "iOS Simulator"') runC('killall "Simulator"') runC('xcrun simctl list > deviceList.txt') lines = File.open('deviceList.txt').readlines lines.each do |line| lineStripped = line.strip if (lineStripped=~/[a-zA-Z0-9]{8}-[a-zA-Z0-9]{4}-[a-zA-Z0-9]{4}-[a-zA-Z0-9]{4}-[a-zA-Z0-9]{12}/) if (lineStripped !~ /unavailable/ && lineStripped.include?("#{deviceDestinationName} (")) puts "Inspecting simulator: #{lineStripped} by making sure it is shut down, then erasing it." needsShutdown = !lineStripped.include?('Shutdown') aDeviceId = lineStripped[/[a-zA-Z0-9]{8}-[a-zA-Z0-9]{4}-[a-zA-Z0-9]{4}-[a-zA-Z0-9]{4}-[a-zA-Z0-9]{12}/] if (needsShutdown) runC("xcrun simctl shutdown #{aDeviceId}") end runC("xcrun simctl erase #{aDeviceId}") #does not work to just uninstall the app with Xcode 7, do just rely on resetting the device above #`xcrun simctl uninstall #{aDeviceId} com.animoto.AppServiceClientTester` end end end runC('rm deviceList.txt') end #Runs a command and prints out both the command that will be run and the results def runC(command) puts '$ ' + command puts `#{command}` end 

目标名称和模拟器应用程序名称似乎已经改变了一点点到xCode6 / iOS8。 这是Cameron Brown的xCode6 / iOS8 osascript的更新版本:

 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 

有请,

权威的iOS模拟器重置脚本(链接)

在这里输入图像描述

基于Oded Regev的代码(基于Jacob Rus的“menu_click”代码)