我需要大量重置iPhone Simulator,并且还没有找到一种无需使用鼠标的方法。这是一件小事,但是我真的很讨厌这样做,很想有一种使用键盘快捷键来做到这一点的方法。
更好的方法是从命令行重置它,因此我可以将重置构建到部署脚本中。
我对iOS或MacOS不太熟悉。
我需要大量重置iPhone Simulator,并且还没有找到一种无需使用鼠标的方法。这是一件小事,但是我真的很讨厌这样做,很想有一种使用键盘快捷键来做到这一点的方法。
更好的方法是从命令行重置它,因此我可以将重置构建到部署脚本中。
我对iOS或MacOS不太熟悉。
xcrun simctl erase all
吗?
Answers:
只需在终端中运行此命令:
xcrun simctl shutdown all && xcrun simctl erase all
@txulu建议的另一种方法是在擦除模拟器之前终止进程:
killall "Simulator" 2> /dev/null; xcrun simctl erase all
Unable to erase contents and settings in current state: Booted
也许还有其他东西可以在您的命令之前/之后通过管道发送到shutdown / reboot sim吗?
killall "Simulator" 2> /dev/null; xcrun simctl erase all
xcrun simctl shutdown all
在此之前尝试xcrun simctl erase all
在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设备
all
参数。因此,您可以使用xcrun simctl erase all
以为我会将其发布给遇到相同需求的任何人。reddit上的某个人给了我这个解决方案(我测试了它,效果很好)。请注意,这次您需要在“设置”之后加上省略号,而不是三个句点(很奇怪)。
这是一个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
FunctionalTests/script:34:49: execution error: File iPhone Simulator wasn’t found. (-43)
,而FunctionalTest /是脚本所在的文件夹
复制粘贴答案-注意:将重置所有可用模拟器的内容和设置。
感谢@Alpine的启发和知识。如果您在命令行中运行此命令,则应该能够重置所有可用的SIM卡。这适用于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
xcrun simctl list | awk -F "[()]" '{ for (i=2; i<NF; i+=2) print $i }' | grep '^[-A-Z0-9]*$' | xargs -I uuid xcrun simctl erase uuid
xcrun simctl erase all
我用XCode 9进行了检查。要关闭所有活动的模拟器,请运行:
xcrun simctl shutdown all
要重置所有模拟器,请运行:
xcrun simctl erase all
您可以像这样过滤要关闭/重置的模拟器:
xcrun simctl shutdown F36B238F-3ED6-4E10-BB5A-0726151916FA
xcrun simctl erase F36B238F-3ED6-4E10-BB5A-0726151916FA
在您的机器上找到所有可访问的模拟器(及其GUID),如下所示:
xcrun instruments -s
要通过GUID运行任何模拟器,请执行以下操作:
xcrun instruments -w F36B238F-3ED6-4E10-BB5A-0726151916FA -t Blank
要将应用程序安装到引导的模拟器中:
xcrun simctl install booted /path/to/your.app
要从启动的模拟器中删除应用程序:
xcrun simctl uninstall booted /path/to/your.app
要在启动的模拟器中启动应用程序:
xcrun simctl launch booted "com.app.bundleIdentifier"
“ com.app.bundleIdentifier”是您在Info.plist中的CFBundleIdentifier
安装Xcode后,我总是在模拟器中为“重置内容和设置”创建键盘快捷键。一个非常有用的节省时间的工具。
System Preferences > Keyboard > Shortcuts > App Shortcuts > "+"
在应用程序选择器中,选择“其他...”以打开应用程序选择器对话框。
在此对话框中您无法“显示包内容”,探索一个名为.app,所以你需要使用Go to Folder
通过Cmd- - 。Shift G(首先打开应用程序下拉菜单,然后选择Other
)
在当前版本的Xcode中,转到路径:
/Applications/Xcode/Contents/Developer/Applications
选择Simulator.app
并按“添加”
对于Menu Title
,输入Reset Content and Settings...
对于Keyboard Shortcut
,按CMD- Shift-R
键盘快捷键解决方案不再适用,很不幸,@ Cameron解决方案也对我不起作用(我尝试调试时没有运气)
这对我有用:
#!/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
tell application "System Events" tell process "iPhone Simulator" tell window 1 click button "Reset" end tell end tell end tell
我编写了一个脚本,该脚本将重置iOS Simulator的所有版本和设备的内容和设置。它从菜单中获取设备名称和版本号,因此它将包括Apple为其发布模拟器的任何新设备或iOS版本。
手动运行或在构建脚本中使用都很容易。我建议在构建之前将其作为操作前运行脚本添加。
它很大程度上基于上述Stian的脚本,但是在新的iOS版本中不会过时,并且消除了对话框(更好的自动化构建脚本和从命令行工作)。
我发现这个非常有用的工具叫做“ 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 "[A-Z]" "[a-z]")
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
我们使用以下python脚本在构建服务器上重置模拟器。
#!/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 e.g. 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()
假设您的应用程序的包标识符设置为环境变量,例如
export BUNDLE_IDENTIFIER=com.example.app_name
也许您想以其他方式传递捆绑标识符。
基于上面的大多数答案,我正在使用Keyboard Maestro,并做了一个小小的Macro来重置当前正在运行的Simulator并重新启动它。重置并重新启动后,它将焦点重新回到Xcode,所以我可以立即Command+R
再次点击以重新运行该应用程序,我觉得非常方便。
红宝石脚本的内容是:
#!/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,因为Xcode 7命令行工具破坏了xcrun simctl卸载命令。我有一个自定义的runC方法,因为我希望看到实际的终端命令及其输出。
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