3

I'd like to launch Xamarin.iOS app on an iPhone simulator on macOS via command line (or VS Code). Is there any way to do it in 2019?

I found that it's possible to build Xamarin app in this question. There's also this question but doesn't work on macOS. But is it possible to just launch (not necessarily debug) from command line? That would enable me to prepare config for VS Code.

I'm using Xamarin.iOS 12.14.0.114

Dominik Roszkowski
  • 2,715
  • 1
  • 19
  • 46

3 Answers3

6

Yes, it is possible using mlaunch tool in the way that Visual Studio does. It is still closed source but by reading its help page and xamarin-macios code. There is no need for IPA build but simply build for debug environment, use mlaunch for installing the app to the simulator/device and run it afterward.

Example: install and launch app after msbuild

/Library/Frameworks/Xamarin.iOS.framework/Versions/Current/bin/mlaunch --launchsim=bin/iPhoneSimulator/Debug/IOS_PROJECT_NAME.app --device::v2:runtime=com.apple.CoreSimulator.SimRuntime.iOS-12-4,devicetype=com.apple.CoreSimulator.SimDeviceType.iPhone-XR

You should see Press enter to terminate the application message when the command is executed.

Explain

  1. msbuild /t:Build

  2. Locate the mlaunch usually in /Library/Frameworks/Xamarin.iOS.framework/Versions/Current/bin/mlaunch. Just find your Xamarin.iOS.framework path

  3. List all available simulator with its runtime and device type value

mlaunch --listsim simulators.xml

Open the output file simulators.xml and select a simulator, i.e. iPhone XR, keep the value of SimRuntime SimDeviceType for the next step

  1. Install your debug Xamarin.iOS app bundle and launch on the simulator
mlaunch --launchsim=[IOS_APP_PATH] --device::v2:runtime=[SimRuntime],devicetype=[SimDeviceType]
  • --launchsim is the relative path to your Xamarin.iOS app bundle built by msbuild task in the first step, usually bin/iPhoneSimulator/Debug/IOS_PROJECT_NAME.app
  • --device composes of SimRuntime and SimDeviceType that you got from the XML file.
Tri Nguyen
  • 480
  • 3
  • 16
0

You are looking for the simctl command, it is available via xcrun helper. You can create, start, shutdown simulator devices and once one is "booted", you can install and launch your iOS .app:

xcrun simctl
usage: simctl [--set <path>] [--profiles <path>] <subcommand> ...
       simctl help [subcommand]
Command line utility to control the 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.
    clone               Clone an existing device.
    upgrade             Upgrade a device to a newer runtime.
    delete              Delete a device or all unavailable devices.
    pair                Create a new watch and phone pair.
    unpair              Unpair a watch and phone pair.
    pair_activate       Set a given pair as active.
    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.
    addmedia            Add photos, live photos, videos, or contacts to the library of a device.
    install             Install an app on a device.
    uninstall           Uninstall an app from a device.
    get_app_container   Print the path of the installed app's container
    launch              Launch an application by identifier on a device.
    terminate           Terminate an application by identifier on a device.
    spawn               Spawn a process by executing a given executable on a device.
    list                List available devices, device types, runtimes, or device pairs.
    icloud_sync         Trigger iCloud sync on a device.
    pbsync              Sync the pasteboard content from one pasteboard to another.
    pbcopy              Copy standard input onto the device pasteboard.
    pbpaste             Print the contents of the device's pasteboard to standard output.
    help                Prints the usage for a given subcommand.
    io                  Set up a device IO operation.
    diagnose            Collect diagnostic information and logs.
    logverbose          enable or disable verbose logging for a device
SushiHangover
  • 73,120
  • 10
  • 106
  • 165
0

I wasn't able to find the option to use mlaunch as suggested by Tri above.

But as SushiHangover mentioned above, you can just run this in your terminal:

xcrun simctl install booted Sample.iOS.app

and just replace Sample with your project's name. I have write a more detailed explanation here.

Also bonus, you can use this to create a build:

msbuild Sample.iOS/Sample.iOS.csproj /verbosity:normal /t:Rebuild /p:Platform=iPhoneSimulator /p:Configuration=Debug
Saamer
  • 4,687
  • 1
  • 13
  • 55