قالب وردپرس درنا توس
Home / IOS Development / Simctl – NSHipster

Simctl – NSHipster



At the heart of Apple's creation myth
is the story of Steve Jobs's visit to Xerox PARC in 1979.
So inspired by the prototype of the Alto computer he was there,
like Prometheus –
Steve Jobs wanted to steal fire from the gods and bring it to the masses
using Lisa and Macintosh computers a few years later.

Like so many creation myths,
This is based on
dubious historical authenticity.
But this is the story we like to tell ourselves
because the mouse and graphical user interface were actually revolutionary.
With a mouse, everyone can use a computer,
even if they are not "computer person".

… but if you is a "computer person"
You probably use the hot keys for your most common operations,
Like to save with S instead of File> Save.
More adventurous people can even venture into the Utilities folder
and brave copy-lime commands in Terminal
for dedicated tasks such as renaming.
This is how many programmers get their start;
When you see the power of automation,
It's hard not to prolong it to do more and more.

That brings us to today's theme:
If you create programs,
You spend a good deal of your time in Xcode
with simulator running in the background.
After exploring the basic functionality of the simulator,
You may feel forced to automate the more time-consuming tasks.
simctl command line utility gives you the interface you need
to use and configure the simulator in a programmatic manner.


If you have installed Xcode,
simctl is available through the command xcrun
(which routes binary executables to the active copy of Xcode on your system).
You can get a description of the tool
and a complete list of sub commands by running simctl
without arguments:

Many of these sub commands are self explanatory
and provide a command line interface for functionality
available through the Simulator app itself.
But the best way to understand how they all work together
is to start with the list sub command.

Managing simulation devices

Run the list sub command for a list of available
Drives, Device Types, Devices, and Device Par:

  $  xcrun simctl list
 - iOS 1
2.1 - iPhone 5s (CC96B643-067E-41D5-B497-1AFD7B3D0A13) (Exit) iPhone 6 (7A27C0B9-411A-4BCD-8A67-68F00DF39C1A) (Exit) iPhone 6 Plus (A5918644-8D46-4C67-B21E-68EA25997F91) (Exit) iPhone 6s (1AB5A4EB-2434-42E4-9D2C-42E479CE8BDC) (Exit) ...

Each device has an affiliate
UUID.
You send this to any of simctl sub commands that take device parameter.

Such a sub command is boot ,
which starts up the specified device,
make it available for interaction:

  $  xcrun simctl boot  $ UUID 

To see the booted simulator in action,
open the Simulator app with the command open as follows:

  $  open /Applications/Xcode.app/Contents/Developer/Applications/Simulator.app/

You can see which devices are booted
by redirecting the end of list to a handle search for the term "Booted":

  $  xcrun simctl list |  grep  Booted
 iPhone X (9FED67A2-3D0A-4C9C-88AC-28A9CCA44C60) (Booted)

To isolate the device identifier,
You can redirect to grep again to search for a UUID pattern:

  $  xcrun simctl list devices |   
      grep    "(Booted)"  |    
      grep    -E    -o    -i    "[[0-9a-f] {8} - {[0-9a-f] {4} -) {3} [0-9a-f] {12}) "
  9FED67A2-3D0A-4C9C-88AC-28A9CCA44C60

Please check - json or -j the alternative to list and other subcommands
to format output as JSON.
Having a structured representation of resources as this is practical
when writing scripts or simulator management programs. [

{
[name] :
"devicetypes" :

] ] [/19659043] Xcode-beta.app /


"bundlePath" : " / Applications ] 19659043] Content / Develops / Platform / iPhoneOS.platform [19659044] / Developer / Library / CoreSimulator / Profiler / DeviceTypes / [19659045] iPhone X.simdevicetype "
" identifier " : "com.apple.CoreSimulator.SimDeviceType.iPhone-X"
},

]
}

When you are done with a device,
You can close and delete the content
by using closure and delete sub commands:

  $  xcrun simctl closure  $ UUID 
  $  xcrun simctl erase  $ UUID [19659014] If you work a lot with Simulator (and have done it for a while),
You probably have accumulated a significant cache of old devices -
many of which can not be used in the latest versions of iOS.
If this sounds familiar, run the following command
can save you a few gigabyte disk space: 

  $  xcrun simctl delete unavailable

Copy and Storage between Desktop and Simulator

It's easy to feel "trapped" when using Simulator,
Can not communicate directly with the operating system as you would any other app.
Recent versions of Simulator have improved this
by allowing you to automatically sync panels
between desktop and simulated device.
But sometimes it is more convenient to do this programmatically,
which is when you want to break out pbcopy and pbpaste subcommands.

Please note that the semantics of these sub commands
may be the opposite of what you expect:

  • pbpaste takes the subject content of the simulator
    and copy them to the desktop's main cake
  • pbcopy takes the most important table of contents from your desktop
    and copy them to the color plate of the device in the simulator

For example,
if you want to copy the contents of a file on my desktop
to the table of a simulated device,
You want to use pbpaste sub command as follows:

  $  pbcopy ~ / Desktop / file.txt
 $  xcrun simctl pbpaste booted

After running these commands,
The contents of file.txt will be used
Next time you paste into Simulator.

Opening URLs in Simulator

You can use the openurl sub command
to have Simulator open the specified URL
on a particular device.

This URL may be a website,
like this:

  $  xcrun simctl openurl started  "https://nshipster.com/simctl" 

... or it may be a custom form associated with an app,
for example map: // for Apple Maps:

  $  xcrun simctl openurl booted maps: //? s  =  Apple + Park

Add to Photo Stream

We've all been there before:
develop a profile image upload
and becomes too familiar with hand-held images
of flora from San Francisco and Iceland that comes pre-loaded on Simulator.

Fortunately, you can mix things up
by using addmedia sub command
to add a photo or movie to the Photos library of the specified simulator:

  $  xcrun simctl addmedia booted ~ / Desktop / mindblown.gif

Alternatively,
You can drag and drop files from the Finder to the Photos app in the simulator
to achieve the same effect.

Capture Video Recording from Simulator

Sure, you can use the global macOS shortcut
to capture a screenshot of a simulator
( 4 ),
but if you intend to use them for your application,
you are much better off using io sub command:

  $  xcrun simctl io started screen app screenshot.png

You can also use this sub command to record a video
As you interact with your app from the simulator
(or not, in the case of automated user interfaces):

  $  xcrun simctl io booted recordVideo app preview.mp4

Enter Simulator Locale

One of the most difficult tasks for developers who locate their app
Switches back and forth between different locations.
This process usually involves manually tapping into the Settings app,
navigates to General> Language and Region
Choose a region from a long modal list
and then waiting for the device to restart.
Each time takes about a minute -
or maybe longer if you return from an unknown location
你 懂 中文 吗? )

But knowing how simulators work in Xcode lets you automate this process
in ways that do not directly involve simctl .

For example,
Now that you know that each simulator device has an UUID,
You can now find the data directory for a device in ~ / Library / Developer / Core Simulator / Devices / .
Simulator settings file
Data / Library / Preferences / .Global Preferences.plist
contains a variety of global settings

You can see the contents of this using the command plutil :

  $  plutil  -p  ~ / Library / Developer / CoreSimulator / Devices /  $ UUID  /data/Library/Preferences/.GlobalPreferences.plist

Here's what to expect, in JSON format:

  {
    "AddingEmojiKeybordHandled" :    true  
    "AppleLanguages" :    ] 
    "AppleLocal" :    "en_US"  
    "AppleITunesStoreItemKinds" :   [   ... [19659033]] 
    "AppleKeyboards" :   [ 
   "no @ sw = QWERTY; hw = Automatic"   
   "emoji @ sw = Emoji"  
      "no @ sw = QWERTY; hw = Automatic"  
 ] 
    "AppleKeyboardsExpanded" :    1  
    "ApplePasscodeKeyboards" :   [

] "no" "emoji" ]
"AKLastIDMSEnvironment" : 0
"PKKeychainVersionKey" : 4
}

You can use e command plutil again
to change the settings programmatically.
For example, the following shell script
Changes the current language and language to Japanese:

  PLIST   =   "/ Library / Developer / CoreSimulator / Devices /   $ UUID   /data/Library/Preferences/.GlobalPreferences.plist" 19659157] LANGUAGE   =  "Yes" 
  LOCALE   =  "Yes_JP" 

plutil  -replace  AppleLocale  string    $ LOCALE    $ PLIST 
plutil  -replace  AppleLanguages ​​ -json    "[  "   $ LOCALE    "]"    $ PLIST 

You can use this same technique to adjust accessibility settings
( com.apple.Accessibility.plist ),
for example, to enable or disable voice over and other assistant technology.

In order to take full advantage of all available settings,
Run plutil -p on all property listings in the Preferred Directory:

  $  plutil  -p  ~ / Library / Developer / CoreSimulator / Devices /  $ UUID  / data / Library / Options /  *  Plist


At least part of credit for the popularity and success of iOS
as a development platform can be given
the power and convenience offered by Simulator.

To test your app on dozens of different devices and operating systems
is something we can take for granted,
but is a magnitude better than the experience on other platforms.
And further the ability to script and potentially automate these interactions
provides an even better developer experience.


Source link

Share