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

问题描述 投票:0回答:1

在我的脚本中,我希望这些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总是一个空字符串,这样脚本会在引导轮询循环中被捕获,直到超时,即使sims明显启动

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

然后如果我在启动sims时再次运行脚本,RESULT非空并且包含字符串com.apple.springboard.services,表示sims已启动。

所以我真的不确定为什么如果我在启动sims之前运行脚本,RESULT是一个空字符串,如果我在启动sims后运行脚本RESULT非空。

资源

How to tell sim has booted

ios xcode bash ios-simulator
1个回答
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)

之后,您可能希望对脚本执行一些其他故障排除。

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

© www.soinside.com 2019 - 2024. All rights reserved.