
剛接觸 iOS 那會兒,我就一直好奇:模擬器上能不能直接安裝 app?要是能,我們乾脆在模擬器上聊 QQ、刷微信得了。直到昨天和朋友聊起這個話題,沒想到——還真能給模擬器「安裝」app!
先說說什麼情況下會產生「在模擬器上裝 app」的需求。
大公司的源碼管理制度嚴格,非開發人員根本接觸不到源碼;蘋果開發證書的管理也同樣森嚴,連開發人員自己都沒有發佈證書,證書只存在於持續集成環境或 App Store 產線裡,或者只攥在最後打包上架的人手上。
于是就出現了這樣的需求:開發人員把 UI 搭好後,要把開發完成的 Alpha 版交給 UI 設計師評審,看看是否完全達標,不達標就打回重做。
一般做法是直接拿真機裝一遍看效果。可要是設計師和開發不在同一地——一個在北京、一個在上海,這下就沒法裝了;源碼又沒法導出讓對方跑 Xcode 模擬器;打 release 的 ipa 靠掃碼安裝吧,公司一大,UDID 早被用光了,也裝不上。這時候就比較頭疼了(一般也沒人碰上這麼蛋疼的情況)。
於是,「給模擬器安裝 app」的需求就誕生了:開發人員該怎麼把開發版的 app 打包出來,交給別人的模擬器安裝呢?
思路很簡單:想讓別人的模擬器跑起我們開發的 app,最直接的辦法就是把 DerivedData 裡的數據拷到對方模擬器上。當然還得考慮到——設計師未必會命令行操作,所以流程越傻瓜越好。
Mac 的拷貝命令有 cp 和 ditto,建議用 ditto。
Usage: ditto [ <options> ] src [ ... src ] dst
<options> are any of:
-h print full usage
-v print a line of status for each source copied
-V print a line of status for every file copied
-X do not descend into directories with a different device ID
-c create an archive at dst (by default CPIO format)
-x src(s) are archives
-z gzip compress CPIO archive
-j bzip2 compress CPIO archive
-k archives are PKZip
--keepParent parent directory name src is embedded in dst_archive
--arch archVal fat files will be thinned to archVal
--bom bomFile only objects present in bomFile are copied
--norsrc don't preserve resource data
--noextattr don't preserve extended attributes
--noqtn don't preserve quarantine information
--noacl don't preserve ACLs
--sequesterRsrc copy resources via polite directory (PKZip only)
--nocache don't use filesystem cache for reads/writes
--hfsCompression compress files at destination if appropriate
--nopreserveHFSCompression don't preserve HFS+ compression when copying files
--zlibCompressionLevel num use compression level 'num' when creating a PKZip archive
--password request password for reading from encrypted PKZip archive
Ditto 比 cp 強的地方在於:
它在拷貝時不僅保留源文件或文件夾的屬性與權限,還能保留資源分支結構與文件夾的源結構。
能確保文件或文件夾被如實複製。
若目標不存在,ditto 會直接複製或新建;而對已存在的文件,命令會與目標合並。
ditto 還能提供完整的符號鏈接。
那麼,把我們本地的 debug 包拷出來:
ditto -ck --sequesterRsrc --keepParent `ls -1 -d -t ~/Library/Developer/Xcode/DerivedData/*/Build/Products/*-iphonesimulator/*.app | head -n 1` /Users/YDZ/Desktop/app.zip
有幾點要說明:
命令最後那個路徑(/Users/YDZ/Desktop/app.zip)是自定義的,我這裡是直接放桌面。除了改這裡的路徑,前面都不用動,連 * 也不用改。
再說說命令裡的 *。打開本地的 ~/Library/Developer/Xcode/DerivedData/ 你會發現,裡面裝的都是曾在本地模擬器跑過的 app,前面是 Bundle Identifier,橫線後面是一串字符串。上面 ditto 裡帶 * 的那段路徑,是用來動態匹配地址的,* 在這裡就是通配符;後面的 head 說明了匹配規則——找出最近一次運行模擬器的 app 路徑。
為了保證打包正確,建議先跑一下要打包的 app(一般 Scheme 裡的 Run 都是 debug product,若這裡改過就換成對應 debug 的 Scheme),確認它就是你要交給設計師審核的那個,再執行 ditto 命令。
跑完上面的 ditto 命令會生成一個 zip,解壓後得到一個 app 文件,這就是 debug 包,也就是要交給設計師的 app 了。
怎麼讓設計師傻瓜式地裝上這個 app?
這裡介紹一個命令行工具:ios-sim。
ios-sim 是能在命令行操控 iOS 模擬器的工具。有了它,我們可以啟動模擬器、安裝 app、啟動 app、查詢 iOS SDK,就像做自動化測試一樣,全程不用打開 Xcode。不過它只支持 Xcode 6 以後的版本。
安裝 ios-sim:
$ npm install ios-sim -g
說明文檔:
Usage: ios-sim <command> <options> [--args ...]
Commands:
showsdks List the available iOS SDK versions
showdevicetypes List the available device types
launch Launch the application at the specified path on the iOS Simulator
start Launch iOS Simulator without an app
install Install the application at the specified path on the iOS Simulator without launching the app
Options:
--version Print the version of ios-sim
--help Show this help text
--exit Exit after startup
--log <log file path> The path where log of the app running in the Simulator will be redirected to
--devicetypeid <device type> The id of the device type that should be simulated (Xcode6+).
--stdout, --stderr, --sdk, --family, --retina, --tall, --64bit ...
用法不難:
ios-sim launch /Users/YDZ/Desktop/app.app --devicetypeid iPhone-6s
其中 /Users/YDZ/Desktop/app.app 是設計師收到 app 後的路徑;--devicetypeid 後面指定一個模擬器版本。
只需把這條命令發給設計師,無腦粘到命令行,裝好 app 的模擬器就會自動啟動並打開 app。
好奇的的同學大概不會滿足於只給模擬器裝 debug 包吧。既然不靠代碼就能給模擬器裝 app,那能不能裝 release 包呢?我好奇地試了一把。
先從 App Store 下載最新微信,把 ipa 後綴改成 zip,解壓,把 Payload 文件夾裡的「WeChat」取出來,再跑 ios-sim 命令。
結果微信確實裝進了模擬器。可一點擊 app,看到月亮界面就退了,控制台打印了一大堆信息。
An error was encountered processing the command (domain=FBSOpenApplicationErrorDomain, code=1): The operation couldn’t be completed. (FBSOpenApplicationErrorDomain error 1.)
...
com.apple.CoreSimulator.SimDevice...launchd_sim (UIKitApplication:com.tencent.xin[0xdf6d][19774]): Program specified by service does not contain one of the requested architectures:
...
SpringBoard: Unable to get pid for 'UIKitApplication:com.tencent.xin[0xdf6d]': No such process (err 3)
...
仔細看 log,根本原因還是在於:
com.apple.CoreSimulator.SimDevice...launchd_sim (UIKitApplication:com.tencent.xin[0xdf6d][19774]): Program specified by service does not contain one of the requested architectures:
Unable to get pid for 'UIKitApplication:com.tencent.xin[0xdf6d]': No such process (err 3)
因為 release 包打包時的 architectures 並不包含模擬器的架構,而 debug 包裡則有。所以 release 包沒法裝到模擬器上。
由於筆者對逆向沒什麼研究,也無法再往下深入。不知逆向技術能否把 release 包破殼後轉成 debug 包?若能轉,通過 ios-sim 命令應該也能直接裝進模擬器。
至此,ios-sim 給模擬器裝 app 的嘗試就告一段落。因為只能裝 debug 包,所以標題裡才給「安裝」加了雙引號——並不是所有 app 文件都能裝進模擬器。