為什么Thera需要iOS模擬器伐庭?
因為想為動態(tài)方案編寫打造原生開發(fā)體驗。
在Thera環(huán)境中分冈,點擊運行拉起模擬器和喚醒預(yù)覽圾另,這一切就跟使用Xcode 編寫自己的Native App一樣。再配合hot-reload
技術(shù),實時響應(yīng)渲染你正在編寫的weex動態(tài)語言代碼文件雕沉。不需要重新編譯集乔、鏈接、打包坡椒,所見即所得扰路。開發(fā)效率得到了真正的提高
另外 在iOS自動化測試中,不同分辨率/不同系統(tǒng)版本等客觀條件要求的測試環(huán)境對企業(yè)是不小的負(fù)擔(dān)倔叼。然而模擬器沒有這種問題汗唱,你可以任意創(chuàng)建iPhone4/iPhone5/iPhone6/iPhone 6 Plus等,然后指定內(nèi)部運行的系統(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 項目中關(guān)于iOS模擬器的運用肥印,都是基于Apple Xcode提供的CLIxcrun
下的子命令simctl
常用的幾個命令是 :
list
加-j參數(shù)识椰,會以JSON的數(shù)據(jù)形式輸出。方便程序處理
{
"state" : "Shutdown",
"availability" : "(available)",
"name" : "iPhone 5",
"udid" : "C0A814A0-C5C1-49E8-97EB-1E4607A08B37"
}
這里面最值得注意的就是 udid
深碱,因為下面的啟動操作是和這個設(shè)備標(biāo)識符關(guān)聯(lián)的
boot
使用xcrun simctl list
獲取設(shè)備UDID之后腹鹉,可以啟動模擬器。這個命令有一些問題:比如不能正常前臺拉起模擬器敷硅」χ洌可以使用Simulator.app
前臺打開:
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
這個application bundle identifier
xcrun simctl launch booted com.taobao.tmall
這個命令還可以設(shè)置應(yīng)用控制臺的輸出out
和 錯誤err
兩個通道重定向到日志文件中,不設(shè)置path
則直接命令行控制臺打出幽七。
也可以傳入一些啟動參數(shù) xcrun simctl launch booted com.taobao.tmall -DumplingsPort 7001
景殷。對應(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;
}
}];