如何异步启动iOS模拟器并使用Bash脚本等待启动完成事件

时间:2018-12-20 17:49:11

标签: ios xcode bash ios-simulator

在我的脚本中,我希望这些iOS模拟器异步启动到主线程

boot B13D4F22-AA4E-4890-8C2B-3C5B7B6E3678 &
boot 5E2588E9-38B5-48AF-99C5-DEE8A1E6FDA0 &
boot 2C28BD02-18BE-4FC5-94DE-075880E70E60 &
boot 723705CD-B038-44E0-B42E-F1D29A28E85E &

然后我要阻止,直到每个sim卡上的启动事件轮询返回true或超时(以先发生的为准)。

isSimBooted B13D4F22-AA4E-4890-8C2B-3C5B7B6E3678
isSimBooted 5E2588E9-38B5-48AF-99C5-DEE8A1E6FDA0
isSimBooted 2C28BD02-18BE-4FC5-94DE-075880E70E60
isSimBooted 723705CD-B038-44E0-B42E-F1D29A28E85E

这是整个脚本:

#!/usr/bin/env bash

function isSimBooted()
{
    # Poll an iOS simulator for boot status in a time out loop.
    # https://stackoverflow.com/questions/37033405/
    # how-can-i-tell-when-the-ios-simulator-has-booted-to-its-home-screen

    UUID=${1}

    echo "isSimBooted"
    RESULT=$(xcrun simctl spawn ${UUID} launchctl print system | grep com.apple.springboard.services)
    echo "RESULT = "${RESULT}
    counter=$((0))
    while [ "$RESULT" = "" ]; do
        sleep 2
        ((counter++))
        RESULT=$(simctl spawn ${UUID} launchctl print system | grep com.apple.springboard.services)
        echo "waiting on boot event for device ${UUID}, RESULT = "${RESULT}
        if [ $counter -gt 90 ]; then
            echo "device ${UUID} took too long to boot"
            exit 1
        fi
    done
    echo "device ${UUID} booted successfully"
}

function boot()
{
  UUID=${1}
  xcrun simctl boot ${UUID}; open -a Simulator
}

echo "booting"

boot B13D4F22-AA4E-4890-8C2B-3C5B7B6E3678 &
boot 5E2588E9-38B5-48AF-99C5-DEE8A1E6FDA0 &
boot 2C28BD02-18BE-4FC5-94DE-075880E70E60 &
boot 723705CD-B038-44E0-B42E-F1D29A28E85E &

echo "waiting"

isSimBooted B13D4F22-AA4E-4890-8C2B-3C5B7B6E3678
isSimBooted 5E2588E9-38B5-48AF-99C5-DEE8A1E6FDA0
isSimBooted 2C28BD02-18BE-4FC5-94DE-075880E70E60
isSimBooted 723705CD-B038-44E0-B42E-F1D29A28E85E

问题是RESULT始终是一个空字符串,即使sim卡已明确启动,该脚本也会陷入启动轮询循环直到超时为止。

while [ "$RESULT" = "" ]; do
    sleep 2
    ((counter++))
    RESULT=$(simctl spawn ${UUID} launchctl print system | grep com.apple.springboard.services)
    echo "waiting on boot event for device ${UUID}, RESULT = "${RESULT}
    if [ $counter -gt 90 ]; then
        echo "device ${UUID} took too long to boot"
        exit 1
    fi
done

然后,如果我在启动模拟市民时再次运行脚本,则RESULT不为空,其中包含字符串com.apple.springboard.services,表示模拟市民已启动。

所以我真的不确定为什么如果在启动模拟市民之前运行脚本,RESULT是空字符串,并且如果在启动模拟市民之后运行脚本RESULT不是空的。

资源

How to tell sim has booted

1 个答案:

答案 0 :(得分:0)

您似乎大部分都拥有它。您的脚本在while循环内发生错误。您写道:

RESULT=$(simctl spawn ${UUID} launchctl print system | grep com.apple.springboard.services)

但是您应该像在脚本前面那样将xcrun放在simctl前面。

RESULT=$(xcrun simctl spawn ${UUID} launchctl print system | grep com.apple.springboard.services)

之后,您可能需要对脚本进行一些其他的故障排除。

我建议您在终端的命令行中运行脚本以进行故障排除。