[Thera] 关于iOS模拟器的知识点

为什么Thera需要iOS模拟器?

因为想为动态方案编写打造原生开发体验。
在Thera环境中,点击运行拉起模拟器和唤醒预览,这一切就跟使用Xcode 编写自己的Native App一样。再配合hot-reload技术,实时响应渲染你正在编写的weex动态语言代码文件。不需要重新编译、链接、打包,所见即所得。开发效率得到了真正的提高

另外 在iOS自动化测试中,不同分辨率/不同系统版本等客观条件要求的测试环境对企业是不小的负担。然而模拟器没有这种问题,你可以任意创建iPhone4/iPhone5/iPhone6/iPhone 6 Plus等,然后指定内部运行的系统版本是iOS8、iOS9还是最新的iOS10.3等等。。

 ~ xcrun simctl 
usage: simctl [--noxpc] [--set ] [--profiles ]  ...
       simctl help [subcommand]
Command line utility to control the Simulator

For subcommands that require a  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, or videos to the photo 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 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

Thera 项目中关于iOS模拟器的运用,都是基于Apple Xcode提供的CLIxcrun下的子命令simctl

常用的几个命令是 :

list

加-j参数,会以JSON的数据形式输出。方便程序处理

{
        "state" : "Shutdown",
        "availability" : "(available)",
        "name" : "iPhone 5",
        "udid" : "C0A814A0-C5C1-49E8-97EB-1E4607A08B37"
}

这里面最值得注意的就是 udid,因为下面的启动操作是和这个设备标识符关联的

boot

使用xcrun simctl list获取设备UDID之后,可以启动模拟器。这个命令有一些问题:比如不能正常前台拉起模拟器。可以使用Simulator.app 前台打开:
open -n /Applications/Xcode.app/Contents/Developer/Applications/Simulator.app --args -currentDeviceUDID C0A814A0-C5C1-49E8-97EB-1E4607A08B37

install

Usage: simctl install  

唤起模拟器之后,可以将自己的应用装载到模拟器中。
xcrun simctl install booted /Users/guomiaoyou/Library/Developer/Xcode/DerivedData/Tmall4iPhone-dwqfxitnjawridavpwanbryizslt/Build/Products/Debug-iphonesimulator/Tmall4iPhone.app

launch

Usage: simctl launch [-w | --wait-for-debugger] [--console] [--stdout=] [--stderr=]   [  ... ]

--console Block and print the application's stdout and stderr to the current terminal.
Signals received by simctl are passed through to the application.(Cannot be combined with --stdout or --stderr)
--stdout= Redirect the application's standard output to a file.
--stderr= Redirect the application's standard error to a file.
Note: Log output is often directed to stderr, not stdout.

唤起模拟器中的App,靠的是使用com.taobao.tmall 这个application bundle identifier
xcrun simctl launch booted com.taobao.tmall

这个命令还可以设置应用控制台的输出out 和 错误err两个通道重定向到日志文件中,不设置path则直接命令行控制台打出。

也可以传入一些启动参数 xcrun simctl launch booted com.taobao.tmall -DumplingsPort 7001。对应的客户端应用获取参数逻辑是:

 [[[NSUserDefaults standardUserDefaults] dictionaryRepresentation] enumerateKeysAndObjectsUsingBlock:^(NSString * _Nonnull key, id  _Nonnull obj, BOOL * _Nonnull stop) {
            if([key isEqualToString:@"DumplingsPort"]){
                port = obj;
                *stop = YES;
            }
}];

你可能感兴趣的:([Thera] 关于iOS模拟器的知识点)