Home > OS >  How can I build and run an iOS Swift app from the terminal?
How can I build and run an iOS Swift app from the terminal?

Time:12-22

I've recently started learning Swift and SwiftUI for iOS development and I completely understand how to run my apps for testing using XCode. You just click the play button with whatever simulator device is selected. However, I really don't like using XCode. I want to use VSCode for my development. Looking online I can find very complicated tutorials on how to build my project for release on the App store or "Ad-Hoc" from the terminal. But, I don't think this is what I need. I'm wondering if there is a way to build and run my app from the terminal on to a simulator or personal device (connected with USB)? I understand I can just have XCode open on the side and run it from there while developing from VSCode, but, my computer is kind of old and having both those apps open makes my fans go crazy.

CodePudding user response:

You can follow this guide on how to build your project using xcodebuild.

Assuming you are in the directory with the .xcodeproj file, the command will look something like this:

xcodebuild -destination "platform=iOS Simulator,name=iPhone 14 Pro Max,OS=16.0" -scheme YourScheme SYMROOT="./build" build

Of course, you can choose any simulator you like. You can find a list of the simulators by doing xcrun simctl list.

The built app will be located in a folder called build/Debug-iphonesimulator, or build/Release-iphonesimulator, depending on your default configuration. The configuration can be set by the -configuration option.

Now you can launch the simulator. Here is a guide for that. Basically, you just specify the UUID of the simulator (again you can find this using xcrun simctl list), and then launch Simulator.app. Example:

xcrun simctl boot 8F9690AC-FCDE-4913-9BD2-E54B3CC9F6C1
open /Applications/Xcode.app/Contents/Developer/Applications/Simulator.app/

To install the built app, you also need the UUID (see also this answer)

xcrun simctl install 8F9690AC-FCDE-4913-9BD2-E54B3CC9F6C1 build/Debug-iphonesimulator/YourApp.app

Finally, you launch your app by specifying the UUID of simulator, and the bundle ID of your app (source)

xcrun simctl launch 8F9690AC-FCDE-4913-9BD2-E54B3CC9F6C1 com.mydomain.YourAppBundleId
  • Related