[Thera] 關(guān)于iOS模擬器的知識(shí)點(diǎn)

為什么Thera需要iOS模擬器?

因?yàn)橄霝閯?dòng)態(tài)方案編寫打造原生開(kāi)發(fā)體驗(yàn)。
在Thera環(huán)境中,點(diǎn)擊運(yùn)行拉起模擬器和喚醒預(yù)覽,這一切就跟使用Xcode 編寫自己的Native App一樣。再配合hot-reload技術(shù),實(shí)時(shí)響應(yīng)渲染你正在編寫的weex動(dòng)態(tài)語(yǔ)言代碼文件。不需要重新編譯、鏈接、打包,所見(jiàn)即所得。開(kāi)發(fā)效率得到了真正的提高

另外 在iOS自動(dòng)化測(cè)試中,不同分辨率/不同系統(tǒng)版本等客觀條件要求的測(cè)試環(huán)境對(duì)企業(yè)是不小的負(fù)擔(dān)。然而模擬器沒(méi)有這種問(wèn)題,你可以任意創(chuàng)建iPhone4/iPhone5/iPhone6/iPhone 6 Plus等,然后指定內(nèi)部運(yùn)行的系統(tǒng)版本是iOS8、iOS9還是最新的iOS10.3等等。。

 ~ xcrun simctl 
usage: simctl [--noxpc] [--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, 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 項(xiàng)目中關(guān)于iOS模擬器的運(yùn)用,都是基于Apple Xcode提供的CLIxcrun下的子命令simctl

常用的幾個(gè)命令是 :

list

加-j參數(shù),會(huì)以JSON的數(shù)據(jù)形式輸出。方便程序處理

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

這里面最值得注意的就是 udid,因?yàn)橄旅娴膯?dòng)操作是和這個(gè)設(shè)備標(biāo)識(shí)符關(guān)聯(lián)的

boot

使用xcrun simctl list獲取設(shè)備UDID之后,可以啟動(dòng)模擬器。這個(gè)命令有一些問(wèn)題:比如不能正常前臺(tái)拉起模擬器??梢允褂?code>Simulator.app 前臺(tái)打開(kāi):
open -n /Applications/Xcode.app/Contents/Developer/Applications/Simulator.app --args -currentDeviceUDID C0A814A0-C5C1-49E8-97EB-1E4607A08B37

install

Usage: simctl install <device> <path>

喚起模擬器之后,可以將自己的應(yīng)用裝載到模擬器中。
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=<path>] [--stderr=<path>] <device> <app identifier> [<argv 1> <argv 2> ... <argv n>]

--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=<path> Redirect the application's standard output to a file.
--stderr=<path> Redirect the application's standard error to a file.
Note: Log output is often directed to stderr, not stdout.

喚起模擬器中的App,靠的是使用com.taobao.tmall 這個(gè)application bundle identifier
xcrun simctl launch booted com.taobao.tmall

這個(gè)命令還可以設(shè)置應(yīng)用控制臺(tái)的輸出out 和 錯(cuò)誤err兩個(gè)通道重定向到日志文件中,不設(shè)置path則直接命令行控制臺(tái)打出。

也可以傳入一些啟動(dòng)參數(shù) xcrun simctl launch booted com.taobao.tmall -DumplingsPort 7001。對(duì)應(yīng)的客戶端應(yīng)用獲取參數(shù)邏輯是:

 [[[NSUserDefaults standardUserDefaults] dictionaryRepresentation] enumerateKeysAndObjectsUsingBlock:^(NSString * _Nonnull key, id  _Nonnull obj, BOOL * _Nonnull stop) {
            if([key isEqualToString:@"DumplingsPort"]){
                port = obj;
                *stop = YES;
            }
}];
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書(shū)系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容