【问题标题】:How can I reset the iOS Simulator from the command line?如何从命令行重置 iOS 模拟器?
【发布时间】:2011-07-04 18:12:27
【问题描述】:

我需要多次重置 iPhone 模拟器,但没有找到不使用鼠标的方法。这是一件小事,但我真的厌倦了这样做,并且希望有一种方法可以使用键盘快捷键来做到这一点。

更好的方法是从命令行重置它,这样我就可以将重置构建到部署脚本中。

我对 iOS 或 MacOS 不是很熟悉。

【问题讨论】:

  • 调用“重置内容和设置”菜单项
  • 我可以建议将接受的答案更改为@Kappe 的答案,即退出模拟器,然后执行xcrun simctl erase all

标签: ios workflow simulator


【解决方案1】:

只需在终端中运行:

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 也许在您的命令关闭/重启 sim 之前/之后我可以通过管道传输其他内容?
  • 可能是一个 sudo xcrun simctl erase all
  • 您需要更多点赞。但是有一个改进:killall "Simulator" 2> /dev/null; xcrun simctl erase all
  • 是的!新的改进版本很棒。谢谢,干得好!
  • @WlkrShrpe 该状态意味着模拟器处于活动状态,需要在重置之前关闭。在xcrun simctl erase all之前尝试xcrun simctl shutdown all
【解决方案2】:

我用 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

【讨论】:

  • 谢谢。通过运行解决问题: xcrun simctl shutdown all; xcrun simctl 全部擦除
【解决方案3】:

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

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

在应用程序选择器中,选择“其他...”以打开应用程序选择器对话框。

在此对话框中,您无法“显示包内容”来浏览 .app,因此您需要通过 Cmd-Shift 使用 Go to Folder -G。 (先打开应用下拉菜单,选择Other

在当前版本的Xcode中,进入路径:

/Applications/Xcode/Contents/Developer/Applications

选择Simulator.app并按“添加”

对于Menu Title,输入Reset Content and Settings...

对于Keyboard Shortcut,按CMD-Shift-R

【讨论】:

  • 很棒的提示!对于那些无法访问 Simulator.app 路径的人,只需将其应用于“所有应用程序”即可。它不像任何其他应用程序都有“重置内容和设置...”
  • @AutomatorCreator 很好,我以后会这样做。我会很高兴不再找到 Simulator 应用,因为它的位置似乎在每个 Xcode 版本中都会发生变化。
【解决方案4】:

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

感谢@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

【讨论】:

  • 谢谢。我写一行xcrun simctl list | awk -F "[()]" '{ for (i=2; i&lt;NF; i+=2) print $i }' | grep '^[-A-Z0-9]*$' | xargs -I uuid xcrun simctl erase uuid
  • 见@Kappe:xcrun simctl erase all
【解决方案5】:

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

【讨论】:

    【解决方案6】:

    这是一个用于重置目标模拟器的 Rakefile 任务。这适用于 Xcode 7,因为 Xcode 7 命令行工具破坏了 xcrun simctl uninstall 命令。我有一个小自定义 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
    

    【讨论】:

      【解决方案7】:

      基于上述大多数答案,我正在使用键盘大师并制作了一个小宏来重置当前正在运行的模拟器并重新启动它。 重置重启后焦点又回到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
      

      【讨论】:

        【解决方案8】:

        我们使用以下 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
        

        也许您想以另一种方式传递捆绑标识符。

        【讨论】:

          【解决方案9】:

          作为使用 xcrun 命令的额外好处,您可以在使用

          列出之后启动设备
          xcrun simctl list
          

          显示列表后运行:

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

          【讨论】:

            【解决方案10】:

            目标名称和模拟器应用程序名称似乎已更改为 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
            

            【讨论】:

            【解决方案11】:

            在 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 设备

            【讨论】:

            • 如果您没有注意到 Alpine 的警告并决定从正在运行的服务中删除路径,您可以通过终止正在运行的 CoreSimulatorService(您需要 SIGKILL)或重新启动来恢复。
            • 这个答案应该被赞成,因为它是最新的解决方案
            • 为了清除所有模拟器,'Joe Susnick' 的回答及其评论很有用
            • xCode 7 也有一个 all 参数。所以你可以用xcrun simctl erase all删除所有模拟器
            • @JeremyHuddlestonSequoia 希望我早点看到这篇文章和你的评论。我现在正在重新安装 Xcode :(
            【解决方案12】:

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

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

            它很大程度上基于上面 Stian 的脚本,但不会因新的 iOS 版本而变得陈旧,并且消除了对话框(更适合自动化构建脚本和从命令行工作)。

            https://github.com/michaelpatzer/ResetAllSimulators

            【讨论】:

              【解决方案13】:

              我想向Cameron Brown's answer 添加一些内容。为了确保重置了正确的版本(例如 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
              

              【讨论】:

                【解决方案14】:

                我来了,

                The Definitive iOS Simulator Reset Script (link)

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

                【讨论】:

                • 好的,我们明白了。尝试在问题已经接受答案的地方添加 cmets。
                • 很好,虽然这不能仅从命令行工作,这是问题所在。不过干得不错:)
                【解决方案15】:

                键盘快捷方式解决方案不再相关,不幸的是@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
                • 您还必须从系统偏好设置中启用“通用访问”
                • 现在有时我会收到此错误:resetSimulator.scpt:1238:1263:执行错误:系统事件出错:无法获取进程“iOS 模拟器”的窗口 1。无效索引。 (-1719)
                【解决方案16】:

                删除内容

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

                你很高兴。

                【讨论】:

                • 不确定这个。运行此命令后,我开始收到错误消息“iOS 模拟器无法安装应用程序”。尝试在模拟器上从 Xcode 运行应用程序时。错误一直存在,直到我以“正确”的方式重置模拟器。就我而言,它发生在 5.1 模拟器上。
                【解决方案17】:

                以为我会为遇到相同需求的任何人发布此内容。 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
                

                【讨论】:

                • 如果您收到“系统事件出错:辅助设备的访问被禁用。”,请确保在“通用访问”下的系统偏好设置下启用它。
                • 这与 alfredApp 和 Automator 混合在一起,我正在像冠军一样重置 sim。您可以将 Automator 工作流程保存为应用程序,并在大约 4 次击键中运行。
                • 此系统偏好设置似乎已移动。在 Mavericks 下,我在 System Preferences > Security & Privacy > Privacy > Accessibility 下找到了它。
                • 在 Mavericks 中,您可能会看到一个问题,说它找不到“重置”按钮。尝试单击按钮 1 而不是单击按钮“重置”
                • 我收到FunctionalTests/script:34:49: execution error: File iPhone Simulator wasn’t found. (-43),FunctionalTest/ 是脚本所在的文件夹
                猜你喜欢
                • 2016-04-02
                • 2013-04-13
                • 1970-01-01
                • 2015-04-29
                • 1970-01-01
                • 1970-01-01
                • 2018-01-03
                • 2016-09-11
                • 2015-10-04
                相关资源
                最近更新 更多