iOS Simulator手动修改plist配置后复原问题及检测方案咨询
iOS Simulator自动化启动配置问题解决方案
问题背景
我正在搭建用于端到端测试的iOS Simulator自动化启动流程,步骤如下:
- 创建新设备
- 启动设备
- 等待一段时间
- 修改配置
对应的bash脚本:
DEVICE_ID=$(xcrun simctl create "iPhone 14" "iPhone 14" "ios17") xcrun simctl boot $DEVICE_ID sleep 60 plutil -replace restrictedBool.allowPasswordAutoFill.value -bool NO ~/Library/Developer/CoreSimulator/Devices/$DEVICE_ID/data/Containers/Shared/SystemGroup/systemgroup.com.apple.configurationprofiles/Library/ConfigurationProfiles/UserSettings.plist plutil -replace restrictedBool.allowPasswordAutoFill.value -bool NO ~/Library/Developer/CoreSimulator/Devices/$DEVICE_ID/data/Library/UserConfigurationProfiles/EffectiveUserSettings.plist plutil -replace restrictedBool.allowPasswordAutoFill.value -bool NO ~/Library/Developer/CoreSimulator/Devices/$DEVICE_ID/data/Library/UserConfigurationProfiles/PublicInfo/PublicEffectiveUserSettings.plist
初始修改生效,但配置值会在几秒到几分钟后自动复原为YES,疑似存在竞态条件,偶发出现。
1. 检测设备首次启动完成,确保配置修改后不被复原
不要依赖固定时长的sleep,改用动态检测方式确认设备完全启动:
- 检查设备状态:循环执行
xcrun simctl list devices $DEVICE_ID,直到输出中设备状态标记为Booted且连续3次检查结果一致 - 验证关键进程:检查SpringBoard进程是否稳定运行,执行:
until pgrep -f "SpringBoard.*$DEVICE_ID" > /dev/null; do sleep 2 done # 额外等待进程稳定 sleep 5 - 监听配置文件稳定性:在修改前,先循环检查目标plist文件的修改时间,直到10秒内文件未更新,再执行修改:
PLIST_PATH=~/Library/Developer/CoreSimulator/Devices/$DEVICE_ID/data/Containers/Shared/SystemGroup/systemgroup.com.apple.configurationprofiles/Library/ConfigurationProfiles/UserSettings.plist LAST_MODIFIED=$(stat -f "%m" "$PLIST_PATH") STABLE_COUNT=0 while [ $STABLE_COUNT -lt 5 ]; do CURRENT_MODIFIED=$(stat -f "%m" "$PLIST_PATH") if [ $CURRENT_MODIFIED -eq $LAST_MODIFIED ]; then STABLE_COUNT=$((STABLE_COUNT+1)) else STABLE_COUNT=0 LAST_MODIFIED=$CURRENT_MODIFIED fi sleep 2 done
2. 自动检测配置复原,无需人工值守
编写监控脚本,定期检查配置值,若复原则重新修改:
DEVICE_ID="你的设备ID" TARGET_KEY="restrictedBool.allowPasswordAutoFill.value" # 检查并修复配置的函数 check_and_fix() { PLIST_FILES=( "~/Library/Developer/CoreSimulator/Devices/$DEVICE_ID/data/Containers/Shared/SystemGroup/systemgroup.com.apple.configurationprofiles/Library/ConfigurationProfiles/UserSettings.plist" "~/Library/Developer/CoreSimulator/Devices/$DEVICE_ID/data/Library/UserConfigurationProfiles/EffectiveUserSettings.plist" "~/Library/Developer/CoreSimulator/Devices/$DEVICE_ID/data/Library/UserConfigurationProfiles/PublicInfo/PublicEffectiveUserSettings.plist" ) for plist in "${PLIST_FILES[@]}"; do VALUE=$(plutil -extract "$TARGET_KEY" raw "$plist" 2>/dev/null) if [ "$VALUE" = "YES" ]; then plutil -replace "$TARGET_KEY" -bool NO "$plist" echo "$(date +'%Y-%m-%d %H:%M:%S'): 修复了$plist的配置" fi done } # 持续监控,每10秒检查一次 while true; do check_and_fix sleep 10 done
将脚本保存为monitor_sim_config.sh,后台运行:nohup ./monitor_sim_config.sh &
3. 更优的设备真实启动状态校验方法
除基础状态检查外,可结合以下方式:
- 检查系统启动日志:通过Simulator日志确认启动完成:
until xcrun simctl spawn $DEVICE_ID log show --predicate 'eventMessage contains "Boot complete"' --last 1m | grep -q "Boot complete"; do sleep 5 done - 验证文件系统可写性:尝试在设备数据目录创建临时文件,确认系统初始化完成:
TEST_FILE=~/Library/Developer/CoreSimulator/Devices/$DEVICE_ID/data/tmp/test_boot_complete until touch "$TEST_FILE" && [ -f "$TEST_FILE" ]; do sleep 2 rm -f "$TEST_FILE" done rm -f "$TEST_FILE" - 检查Simulator UI状态:用AppleScript判断窗口是否正常加载(仅适用于有UI的环境):
osascript -e ' tell application "Simulator" repeat until window 1 exists delay 2 end repeat end tell '
内容的提问来源于stack exchange,提问作者Nicholas Fitton
相关产品推荐
相关产品推荐

