如何通过命令行从iOS 8模拟器中删除应用程序?

31
我有一个在 iOS 模拟器中运行的自动化应用程序,我需要在执行另一个运行之前将其删除。如何通过命令行从 iOS 模拟器中删除该应用程序?
对于每个模拟器设备目录(位于 `~/Library/Developer/CoreSimulator/Devices/*`),我尝试删除 `./data/Containers/Bundle/Application/` 和 `./data/Containers/Data/Application/`。
即使我长按模拟器中的应用程序并单击 X 按钮来删除应用程序,用户默认设置也没有被清除。我希望应用程序状态是100%干净的。
我找到了一个好的解决方案来解决这个问题。
4个回答

59

使用Xcode 6.1卸载应用程序,请使用以下命令:

xcrun simctl uninstall booted com.example.apple-samplecode.UICatalog

其中com.example.apple-samplecode.UICatalog是您希望卸载的应用程序的捆绑标识符。


这是一个更加清晰的实现,特别是包含了“booted”。 - Glen T
如果没有启动的模拟器,有没有一种静默失败的方式? - tfe
2
我想我可以简单地执行 xcrun simctl uninstall booted com.example.apple-samplecode.UICatalog || true - tfe
3
在多个模拟器设备上同时运行时,卸载应用的设备未定义。在这种情况下,请使用设备的UDID而不是“booted”(使用“xcrun simctl list”获取可用的UDID列表)。 - atineoSE
1
正是我们所需要的!我在开发UITests时搜索了一种方法,每次启动时都可以清除所有内容。 - Pan Mluvčí
作为对@atineoSE建议的补充,如果您只想要正在运行的模拟器列表,请使用以下命令:xcrun simctl list | grep "Booted"。然后,您可以使用以下命令从特定的模拟器中删除应用程序:xcrun simctl uninstall [UDID] [BUNDLE_ID] - Ricardo Barroso

23

我们发现一种删除用户默认设置的方法是删除./data/Library/Preferences/*下的所有文件,以及删除应用和数据目录。

然而,在Xcode 6中,命令xcrun有一个名为simctl的新子命令,允许我管理iOS模拟器,包括重置模拟器和安装应用程序。

我想出的解决方案是使用以下命令:

xcrun simctl erase [device ID]

示例

如果 xcrun simctl list() 返回

9DDA0CFE-7CEC-40B6-A343-1EC01F282B22 (active, disconnected)
    Watch: Apple Watch Series 2 - 42mm (88474523-163E-4021-B591-2AECBFA26997) (Shutdown)
    Phone: iPhone 7 Plus (5785E680-15CD-42D3-82AB-597286A270C5) (Shutdown)

然后运行这两个命令

xcrun simctl erase 88474523-163E-4021-B591-2AECBFA26997
xcrun simctl erase 5785E680-15CD-42D3-82AB-597286A270C5

() 设备 ID 可以通过运行以下命令获得


xcrun simctl list

这将重置模拟器(相当于iOS Simulator > Reset Contents and Settings... 菜单项)。

在 Xcode 6.0.1(版本号为 6A317)中,存在一个错误或行为更改,即卸载应用程序时不会删除用户默认设置。

Usage: simctl [--noxpc] [--set <set path>] <subcommand> ... | help [subcommand]
Command line utility to control the iOS Simulator

For subcommands that require a <device> argument, you may specify a device UDID
or the special "booted" string which will cause simctl to pick a booted device.
If multiple devices are booted when the "booted" device is selected, simctl
will choose one of them.

Subcommands:
    create        Create a new device.
    delete        Delete a device.
    erase         Erase a device's contents and settings.
    boot          Boot a device.
    shutdown      Shutdown a device.
    rename        Rename a device.
    getenv        Print an environment variable from a running device.
    openurl       Open a URL in a device.
    addphoto      Add a photo to the photo library of a device.
    install       Install an app on a device.
    uninstall     Uninstall an app from a device.
    launch        Launch an application by identifier on a device.
    spawn         Spawn a process on a device.
    list          List available devices, device types, or runtimes.
    notify_post   Post a darwin notification on a device.
    icloud_sync   Trigger iCloud sync on a device.
    help          Prints the usage for a given subcommand.

如果你只想删除单个应用程序,你应该使用卸载(uninstall)而不是擦除(erase)。 - Jeremy Huddleston Sequoia
1
在Xcode 6.0.1中,即使您使用卸载命令,用户默认设置仍然保持不变(这是一个错误)。这就是为什么我建议使用擦除的原因。 - Frank
啊,没错。不错的观点,但这并不是 Xcode 6.0.1 的问题,而是 iOS 8.0 的问题。如果你在模拟的 7.x 设备上使用“delete”,它会正常工作。 - Jeremy Huddleston Sequoia

11

一条命令重置所有内容和设置

  1. 退出iPhone模拟器
  2. 在终端中运行:

    xcrun simctl erase all
    

这将重置所有模拟器的内容和设置,仅针对当前版本的Xcode(即由xcode-select -p引用的版本)。


5
xcrun simctl uninstall simulatorIdentifier appBundleId

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接