You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何通过终端构建并运行iOS Swift应用(无需依赖Xcode)

用VSCode配合终端运行iOS应用(模拟器/真机)

一、前置准备

  • 安装Xcode命令行工具:终端执行 xcode-select --install 按提示完成安装
  • 确保项目是标准iOS项目结构(不管是用swift package init创建,还是原Xcode项目,VSCode可直接打开)
  • VSCode安装必要插件:Swift Language Server(提供代码提示)、Swift Debugger(支持调试)

二、运行到模拟器

  1. 查看可用模拟器列表:
    xcrun simctl list devices available
    
    记下目标模拟器的名称(比如iPhone 15)或UDID
  2. 启动目标模拟器:
    xcrun simctl boot "iPhone 15"
    
  3. 进入项目根目录(包含.xcodeproj或.xcworkspace的文件夹),构建并安装应用:
    针对.xcodeproj项目:
    xcodebuild -project YourProject.xcodeproj -scheme YourScheme -destination 'platform=iOS Simulator,name=iPhone 15' build install
    
    针对.xcworkspace项目,替换参数为:-workspace YourProject.xcworkspace
  4. 启动模拟器中的应用:
    从项目的Info.plist中找到Bundle ID(比如com.yourcompany.yourapp),执行:
    xcrun simctl launch "iPhone 15" com.yourcompany.yourapp
    

三、运行到USB连接的真机

  1. 确保真机已信任电脑,且你的苹果开发者账户已添加该设备UDID(个人免费账户只需之前在Xcode中配置过签名,终端会复用该配置)
  2. 查看真机UDID:
    idevice_id -l
    
    若缺少idevice_id命令,用Homebrew安装依赖:brew install libimobiledevice
  3. 进入项目根目录,构建并安装到真机:
    xcodebuild -project YourProject.xcodeproj -scheme YourScheme -destination 'platform=iOS,id=你的真机UDID' -configuration Debug build install
    
    注:个人免费账户仅支持Debug版本构建
  4. 启动真机上的应用:
    idevicedebug run com.yourcompany.yourapp
    

四、简化操作小技巧

将常用命令写成Shell脚本,比如创建run_sim.sh文件,内容如下:

#!/bin/bash
xcrun simctl boot "iPhone 15"
xcodebuild -project YourProject.xcodeproj -scheme YourScheme -destination 'platform=iOS Simulator,name=iPhone 15' build install
xcrun simctl launch "iPhone 15" com.yourcompany.yourapp

给脚本添加执行权限:chmod +x run_sim.sh,之后直接执行./run_sim.sh即可一键完成操作

内容的提问来源于stack exchange,提问作者Ammar Ahmed

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.08 00:55:13