قالب وردپرس درنا توس
Home / IOS Development / # 313: Collaborate with the iOS simulator using simctl 📱🎮 – Little Bites of Cocoa

# 313: Collaborate with the iOS simulator using simctl 📱🎮 – Little Bites of Cocoa



Subjects

Topics

Today we will look at how to control (and interact with) iOS Simulator from the command line.

There is no need to install anything new though. The tool we're using is already on our Mac hiding inside the command xcrun that is being installed with Xcode .

It is called simctl .

Let's begin!

The first thing we want to do is tell simctl to print a list of our current iOS Simulator devices. We can do it by running:

  xcrun simctl list devices

This will print a list of all simulated devices and a UDID value with each:

  - iOS 1
0.3 - iPhone 7 (C4481459-5BB1-4CE1-9BE0-CF0FEA351299) (Booted) iPhone 7 Plus (ADCB6F99-5ADD-49B1-83AE-5391D845C4D0) (Exit) iPhone SE (FB0899C0-5812-492E-80D9-9DE517554C12) (Exit) iPad Pro (9.7 inch) (45F47977-9A03-4DD1-8FD0-289F7936FE98) (Exit) iPad Pro (10.5-inch) (C3C909DC-BF70-4D67-BF7E-A41A0CF4AF56) (Exit) ...

We can see (Booted) the note next to the first device that simctl has identified iOS Simulator we had opened at the run command.

We can send it UDID ( C4481459-5BB1-4CE1-9BE0-CF0FEA351299 ) to other commands to target our iOS simulator .

For shorthand but we can also just send the expression booted to target the current iOS Simulator .

[Notethatin Xcode 9 multiple iOS Simulators can run at one time. If we send bootup in this case, simctl [19659010] just select one for us).

Whew. Okay with that introduction, out of the way, let's try this thing out.

First up, one of the best features of simctl opens URLs .

To open a URL in iOS Simulator from the command line is all we need to run:

  xcrun simctl openurl booted  "https://littlebitesofcocoa.com "]

If we look at our iOS Simulator after running it, we see Safari open and load page.

19659005] Even better, this works as well with custom URLs :

  xcrun simctl openurl booted  "spaceships: // ships / 123"  ]

Very fun.

Then we add some pictures and videos.

We can use addmedia sub command to import media to iOS Simulator :

  xcrun simctl addmedia booted ~ / Desktop / images / image1.png ~ / Desktop / images / image2.jpg ~ / Desktop / images / image3.jpg

We may include one or more file paths here. It supports images, videos and even Live Images .

After driving, the files will be imported and displayed in photo library

:

Next: iCloud Syncing .

We can explicitly force a sync of iCloud using this command:

  xcrun simctl icloud_sync booted

What's nice about this, we'll even get error messages if (for example) iCloud is not configured on this simulated device:

  An error occurred while processing the command (domain = BRCloudDocsErrorDomain , code = 2):
The operation could not be completed.
(BRCloudDocsErrorDomain error 2 - Logged out - iCloud Drive is not configured)

This next is a doozy. We can use simctl to record and stream live video and capture screenshots on any screenshot of our iOS Simulator .

First let's take a screenshot:

  xcrun simctl io booted screenshot ~ / Desktop / screenshot.png

Neat. Simply, next, let's try to record a movie:

  xcrun simctl io booted recordVideo type  =  mp4 ~ / Desktop / movie.mp4

Subscriptions can end | be directed to other commands, or even to a TCP or UDP.

Full-featured movie file is available here for the curious.

recordVideo outlet for live streaming. This works on all iOS tvOS and even watchOS Simulators . Very cool.

Last but not least, we can print the path for the app's installation directory on disk:

  xcrun simctl get_app_container boot com.spaceships.app
  /Users/example/Library/Developer/CoreSimulator/Devices/C4481459-5BB1-4CE1-9BE0-CF0FEA351299/data/Containers/Bundle/Application/49A59051-1404-431C-8B65-B589EC0F6267/Spaceships.app

Nice.

To save us a step, we can | (pipe) the path send to pbcopy to put the value on our clipboard to easily paste later: 19659015] xcrun simctl get_app_container boot com.spaceships.app | pbcopy

There is probably more like simctl can do. To view long list of available commands and features, we can run:

  xcrun simctl

We can add, remove and even reset / delete simulated devices, interact with device devices, launch programs with environment variables and much more.

It's all for now, happy simulation! [19659005] - Update: Tuesday, July 25, 2017 -

Friend of pod Bites? [andthecreatoroftheamazingFastlaneSuite Felix Krause writes with a great addition regarding - json flag:

We can pass - json – – json – you can use to get results in the JSON format .] -j for shorthand) flag to any of simctl commands that print information to print it in JSON format.

This is perfect for | (pipes) – for other commands, or for host / session usage:

  xcrun simctl list runtimes --json

    {19659084] "runtimes"  :   [
      {
        "buildversion"  :    "14E8301"  
        "available" 
    :    "(available)"  
        "name"  :    "iOS 10.3"  
        "identifies"  :    .apple.CoreSimulator. SimRuntime.iOS-10-3 "
      " version " :  " 10.3.1 "
    }, 
      {
      " buildversion " :   " 14V243 " 
       " availability " :   " (available) " 
       " name " :  "  :    "com.apple .CoreSimulator.SimRuntime.watchOS-3-2 " 
       " Version " : [19659089]" 3.2 "
    }  
 ]  
 } [19659128] ]

Source link