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

问题描述 投票:0回答:5

我在 iOS 模拟器中运行了一个自动化程序,我必须在下次运行之前将其删除。如何通过命令行从 iOS 模拟器中删除应用程序?

对于每个模拟器设备目录(位于

~/Library/Developer/CoreSimulator/Devices/*
),我 尝试删除
./data/Containers/Bundle/Application/
./data/Containers/Data/Application/

即使我尝试通过长按模拟器中的应用程序(应用程序变得不稳定)并单击 X 按钮来删除应用程序,用户默认值也没有被清除。我希望应用程序状态 100% 干净。

我找到了一个很好的解决方案来解决这个问题。

ios xcode ios-simulator xcode6 build-automation
5个回答
59
投票

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

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

其中

com.example.apple-samplecode.UICatalog
是您要卸载的应用程序的捆绑包标识符。


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(Build 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.

11
投票

通过单个命令重置所有内容和设置

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

    xcrun simctl erase all
    

这将重置 Xcode 活动版本(由

xcode-select -p
引用的版本)的所有模拟器的内容和设置。


5
投票
xcrun simctl uninstall simulatorIdentifier appBundleId

0
投票

我知道,这是一个十年前的问题,但自那以后情况发生了变化,尽管测试干净安装的必要性仍然存在。上面的答案对我来说似乎都不太令人满意,所以我提出了另一个解决方案。

我提出了一种对于不同设备/模拟器 ID 以及捆绑 ID 来说安全的方法:

if [[ "$RUN_DESTINATION_DEVICE_PLATFORM_NAME" == "iphonesimulator" ]]; then
    xcrun simctl uninstall $RUN_DESTINATION_DEVICE_UDID $PRODUCT_BUNDLE_IDENTIFIER
else
    xcrun devicectl device uninstall app --device $RUN_DESTINATION_DEVICE_UDID $PRODUCT_BUNDLE_IDENTIFIER
fi

将其自动化并作为开发工作流程的一部分运行:

  1. 添加自定义方案,比如说
    CleanInstall
    :

  1. New Run Script Action
    添加到
    Run
    阶段的
    Pre-actions
    :

  1. 确保“提供构建设置”您的目标以获得对
    $PRODUCT_BUNDLE_IDENTIFIER
    的访问权限,这在
    Run
    阶段是无法访问的:

  1. 粘贴上面的脚本

最终的设置应如下所示:


设计考虑,与其他方法的区别:

优点:

  1. 完全自动化:无需手动触摸设备/模拟器,或手动调用终端命令
  2. 不需要第三方工具,例如
    libmobiledevice
  3. 不需要您记住甚至不知道捆绑 ID 或目标设备/模拟器 ID。不需要
    xcrun simctl list
    ,Xcode 为你提供一切
  4. 仅擦除所需的应用程序,不需要您像xcrun simctl erase
    那样完全擦除模拟器
    
  5. 运行应用程序时,从 Xcode 中指定的目标设备/模拟器中准确删除应用程序,这与
  6. xcrun simctl uninstall booted
     不同,其中可能同时打开/连接多个设备/模拟器,并且行为可能是未定义的
  7. 假设您指定了方案,自然会扩展到从 Xcode UI 和命令行进行单元测试
缺点:

    据我所知,这不会(也不应该)删除钥匙串项目。因此,除非您正在测试钥匙串项目/组,否则应该没问题。
  1. 在研究这个问题时,我意识到有些应用程序依赖于 safari 缓存(WKWebView 用户?),并且 safari 缓存有可能不会受到仅应用程序删除的影响,并在此之后继续存在?
在所有这些情况下,请随意创建与上述类似的另一个方案,例如

xcrun simctl erase $RUN_DESTINATION_DEVICE_UDID

。但要小心真实设备!

© www.soinside.com 2019 - 2024. All rights reserved.