Deployment of an iOS app to iTunes Connect is time-consuming process and as an iOS developer (and of course as a human) I always want to make process easy. it’s very common for iOS developers test, build, archive and upload to iTunes Connect with Xcode. but is there any easier process? the answer is yes, xcodebuild commands.
What is the xcodebuild
xcodebuild is command-line tool that allows developers to perform build, query, analyze, test and archive operations on Xcode projects and workspaces from the command-line. It operates on one or more targets contained in our project, or a scheme contained in our project or workspace.
In this post, we will learn more about xcodebuild and find out how it works.
Analysing
The static analyzer tries out thousands of possible code paths in a few seconds, reporting potential bugs that might have remained hidden or bugs that might be nearly impossible to replicate. we can Analyze our project with this command:
$ xcodebuild -project Sample Project.xcodeproj -scheme Sample Project -sdk iphonesimulator15.0 clean analyze
Building
By build command we can simply run our app inside simulators or can be used by test bundle.
Build for running
For running our app in simulator just add this command:
$ xcodebuild build-for-testing -workspace Sample Project.xcworkspace -scheme Sample Project -destination generic/platform=iOS
This will create a derive data inside ~/Library/Developer/Xcode/DerivedData/ directory. Thre are various options that we can pass to override the default settings so that we can control the artifact e.g -destination or -derivedDataPath etc
Build for testing
We can build for testing using command:
$ xcodebuild build-for-testing -workspace Sample Project.xcworkspace -scheme Sample Project -destination generic/platform=iOS
Now that, we can use test-without-building action to run tests without building an app.
Testing
We can run tests using command:
$ xcodebuild -scheme Sample Project -workspace Sample Project.xcworkspace/ test
this command will build the Sample project and run tests.
Archiving
As an iOS developer I have so much pain and headache with archiving process and dealing with provisioning profiles, certificates and build configurations. with a command line we can export out projects in IPA format.
$ xcodebuild -exportArchive -archivePath $PWD/build/Sample Project.xcarchive -exportOptionsPlist exportOptions.plist -exportPath $PWD/build
Note that above command requires a -exportOptionsPlist argument that points to a .plist file with export options. For a complete list of what you can put in that plist, run xcodebuild -help. The minimal contents of the file looks like this:
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd"> <plist version="1.0"> <dict> <key>method</key> <string>app-store</string> <key>teamID</key> <string>YOUR_TEN_CHARACTER_TEAM_ID</string> </dict> </plist>
we run this command successfully, we will have IPA file created using provisioning profile ‘Sample Project Distribution Profile’. At the end we will have our IPA, Sample Project.ipa binary ready to upload to iTunes Connect.
Example
Here is my daily xcodebuild commands:
# 1. Project Test echo '🌑 1.Project test begin...' xcodebuild -project 'MyApp.xcodeproj' -scheme 'MyApp' -destination 'platform=iOS Simulator,name=iPhone 8,OS=14.4' -configuration Release test | xcpretty # 2. Project Archive echo '🌘 2.Project archive begin...' xcodebuild -project 'MyApp.xcodeproj' -scheme 'MyApp' -destination 'generic/platform=iOS' -allowProvisioningUpdates -archivePath 'Archive/MyApp.xcarchive' -configuration Release PROVISIONING_PROFILE='iOS Team Provisioning Profile: *' clean archive | xcpretty # 3. Export project .ipa file echo '🌗 3.Project export begin...' xcodebuild -exportArchive -archivePath 'Archive/MyApp.xcarchive' -exportPath 'Release' -allowProvisioningUpdates -exportOptionsPlist 'Tools/ExportOptions.plist' # 4. Remove Archive folder echo '🌖 4.Remove Archive folder begin...' rm -rf 'Archive' echo '** REMOVING END **' echo '' # 5. Move Release folder to Developer folder echo '🌕 5.Moving Release folder begin...' mv 'Release' 'Final Path' echo '** MOVING END **'