How to build Xcode project from the command line? - ios

I've tried reading the Xcode Tools documentation Apple provides, so that I can use the Terminal to build a .app file and run the resulting app on the Simulator. Essentially what I want to do is do the same thing as Cmd + R does on Xcode.
So far I've attempted to build my .xcodeproj like this:
xcodebuild -configuration Debug build
However, when I install & run it on the Simulator I get an app w/ a black screen:
// Boot device
xcrun simctl boot "iPhone 7"
// Install app
xcrun simctl install "iPhone 7" "/Users/.../MyApp/build/Debug-iphoneos/MyApp.app"
// Open simulator
open /Applications/Xcode.app/Contents/Developer/Applications/Simulator.app
// Launch app using its bundle id
xcrun simctl launch booted "com.example.apps.MyApp"
Not to mention the xcrun simctl launch booted "com.example.apps.MyApp" line never terminates and on the Simulator it keeps trying to open and reopen the app, but the app only ever shows a black screen.
If anyone could tell me what I'm doing wrong with the building of the.xcodeproj that would be great!

After having a working configuration in Xcode, open a shell and navigate to the directory, where your <NAME>.xcodeproj resides.
After running:
xcodebuild -list -project <NAME>.xcodeproj/
you see a list of Schemes.
Copy the desired scheme name and run:
xcodebuild -workspace <WORKSPACE NAME> -scheme <SCHEME NAME> build
You can install [ios-deploy][1] i.e. via:
npm install -g ios-deploy
Copy the app path from the end of the xcodebuild output and run:
ios-deploy --debug --bundle <APP PATH>
Now the app should be launched on i.e. a connected device.
[1]: https://github.com/phonegap/ios-deploy

Related

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

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.
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

Building and launching app in Simulator from terminal

I wanted to build and launch my iOS app using a shell script and not Xcode. My question is the similar to this question: How to launch an iOS app in the simulator without XCode rebuilding the app, which was asked in 2012.
It says that the IPA file created in build directory within the project directory can be used. I could not find a build directory inside my project folder. Also, I want to build the app from a script.
What is the most latest way of building and running an app on simulator using terminal commands?
Edit:
I built the app from command line:
xcrun xcodebuild \
-scheme UITestingTutorial \
-project UITestingTutorial.xcodeproj \
-configuration Debug \
-destination 'platform=iOS Simulator,name=iPhone 11,OS=13.6' \
-derivedDataPath \
build
But how do I launch it now?
Running this script inside your project directory will launch the app programmatically.
xcrun xcodebuild \
-scheme UITestingTutorial \
-project UITestingTutorial.xcodeproj \
-configuration Debug \
-destination 'platform=iOS Simulator,name=iPhone 11,OS=13.6' \
-derivedDataPath \
build
open -a "Simulator"
# This simulator id has been manually found
# by running 'xcrun simctl list' and searching
# for iPhone 11 under iOS 13.6 under Devices
# A script can be written to automatically find this.
xcrun instruments -w 3100E7BC-0B95-4B3E-B0DC-8743FFCB731A
xcrun simctl install 3100E7BC-0B95-4B3E-B0DC-8743FFCB731A ./build/Build/Products/Debug-iphonesimulator/UITestingTutorial.app (http://uitestingtutorial.app/)
xcrun simctl launch 3100E7BC-0B95-4B3E-B0DC-8743FFCB731A com.codepro.UITestingTutorial
You can refer to Launch apps in iOS Simulator using Terminal for more details. Please note the xctool is deprecated.

Xcodebuild test hangs indefinitely without error

Xcodebuild hangs indefinitely in Jenkins. But same setup works properly in local machine.
(Xcode 7.3.1, OS X 10.11.6)
Command executed (After clearing DerivedData & Clean)
xcodebuild -scheme APP_SCHEME -derivedDataPath ./DerivedData -sdk iphonesimulator -destination 'OS=9.3,name=iPhone 6' ARCHS=x86_64 VALID_ARCHS=x86_64 ONLY_ACTIVE_ARCH=NO GCC_GENERATE_TEST_COVERAGE_FILES=YES GCC_INSTRUMENT_PROGRAM_FLOW_ARCS=YES test
Launches app in simulator and then Hangs indefinitely after following command without any error
touch APPSCHEME.xctest
xcodebuild hangs because it is not able to find & launch the appropriate simulator.
Verify whether the below 2 commands gives the matching list of simulators and they are in available state.
- xcrun simctl list
- instruments -s devices
If not, then proceed on to do a clean uninstall of Xcode,install and then try running the command again
Delete Xcode from Applications folder
In ~/Library/Developer/ Delete Xcode and CoreSimulator folder
sudo rm /Library/Preferences/com.apple.dt.Xcode.plist
sudo rm -r ~/Library/Caches/com.apple.dt.Xcode
sudo rm -r ~/Library/Application\ Support/Xcode/
Clean Trash
Restart Install Xcode ,Launch and Accept license Agreement and then Restart

xcode build works on device, but not on simulator

I want to setup a jenkins server to build our app and run javascript/automation tests on a simulator.
Regarding that topic, I had several issues to launch the app on the simulator. Now I am fighting with the hole simulator thing.
My plan is, to build the app and to launch it on the simulator. Here is what Ive done:
Step 1:
(make archive)
xcodebuild -project "myApp.xcodeproj" -scheme "api-dev" -configuration "Debug Dev" -destination 'platform=iOS Simulator,name=iPhone 5s,OS=8.1' archive -archivePath /Users/name/Desktop/build/archive/myApp.xcarchive
Step 2:
(make/export .app file)
xcodebuild -exportArchive -exportFormat app -archivePath /Users/name/Desktop/build/archive/myApp.xcarchive -exportPath /Users/name/Desktop/build/myApp.app
Step 3:
(using xcrun to boot simulator)
xcrun simctl boot < device_id >
Step 4:
(using xcrun to install app on booted simulator)
xcrun simctl install booted /Users/name/Desktop/build/myApp.app
Step 5:
(shutdown simulator, since it would crash when I use instruments afterwards)
xcrun simctl shutdown < device_id >
Step 6:
(start .js tests via instruments)
instruments -w 'iPhone 5s (8.1 Simulator)' -t "/Applications/Xcode.app/Contents/Applications/Instruments.app/Contents/PlugIns/AutomationInstrument.xrplugin/Contents/Resources/Automation.tracetemplate" /Users/name/Desktop/build/myApp.app -e UIASCRIPT /Users/name/Projects/project-name/myAppTests/UITests/script.js
When I open the simulator to launch the installed App (so without Step 6), only to test if I can open and launch the App, it does not work. Thats my real issue at the moment.
Error: "Target failed to run: Failed looking up pid of launched process"
When I open Xcode and take a real device and copy "/Users/name/Desktop/build/myApp.app" on it, it totally works fine...
What do I miss here with the Simulator?
Thank you for your input.

xcode Code to install app

I want to achieve with the code, I write the program to install other applications to the phone, I found online said to be to app unzip and copy to the / var / mobile / Applications directory, I follow suitHowever, the installed program, you must restart the phone to display and install the application can not run
You can combine xcodebuild and xcrun commands to install and run your app.
The xcodebuild compile your project and create a .app file in DSTROOT that um point.
xcodebuild -sdk iphonesimulator8.4 -arch i386 install DSTROOT=~/MyApp
if you are using some pod file you have .xcworkspace instead .xcodeproj then add -workspace and -scheme at your xcodebuild.
xcodebuild -sdk iphonesimulator8.4 -arch i386 -workspace MyApp.xcworkspace -scheme MyApp install DSTROOT=~/MyApp
After created the .app file you can run into a Simulator device.
xcrun instruments -w "iPhone 6 (8.4 Simulator)"
xcrun simctl install booted ~/MyApp/Applications/MyApp.app
if you want to run in another Simulator device, try see all devices available.
xcrun instruments -s

Resources