Track your iOS application code quality using Sonar

This article is the iOS counterpart of Romain’s recent article on tracking code quality with Sonar on Android.

The problematic remains the same: quality is often overlooked for mobile Apps and the cost of setting up a proper quality tracking environment is usually deemed too high. This article will focus on showing that, while the iOS platform is not as tooled as Android, it’s now fast and easy to track code quality on iOS projects.

Even though this has been possible in the past mostly using Java tools (see a preceding post on tracking iOS code quality with Jenkins), it has often been limited to big projects where a quality process has to be enforced (and sometimes some metrics were even part of the contract – even if I am not a big fan of this). The main limitation was in that case the time and expertise to set everything up.

This article follows a presentation we held at soft-shake last October.

Meet SonarQube

For the uninitiated, SonarQube is a continuous quality analysis platform running as a web server that tracks metrics regarding your code and its structure. It can be extended through plugins, and usually embeds useful tools and checks. Historically SonarQube only dealt with Java code but it has been extended since, and it handles most common languages as of today (available features may vary).

Our use case being an iOS application, we are wandering far away from the Java world and there is no official support of this language by SonarSource. Romain Felden and I created a dedicated community plugin – Sonar Objective-C plugin – that enables Objective-C project analysis.

System prerequisites

Quite a few tools will have to be installed on your computer, this is clearly the most time-consuming part. Hopefully you will have to do that only once on your CI server (or locally on your workstation):

  • SonarQube tooling: you have to install SonarQube, SonarQube Runner and SonarQube Objective-C plugin via the standard plugin install procedure
  • Build tooling: xctool, which is replacement for Apple xcodebuild tool with quite a few enhancements. The best way to install it is via HomeBrew. A side note on that point: xctool is only used internally by the plugin to build, test and compute the metrics, it has no impact whatsoever on your project, it will still be built with Xcode/xcodebuild.
  • Analysis tooling: you have to install OCLint (violations) and gcovr (code coverage). gcovr is a simple binary to put somewhere in your PATH.

Tip: You may need to go wild and install a development version of OCLint (0.8/0.9) as in my experience, the stable 0.7 version had blockers on my projects

Project prerequisites

Situation is easier than for Android because there is actually only one build tool: Xcode.

The only things you should do are:

  • copy in your Xcode project root folder (along your .xcodeproj/.xcworkspace file)
  • copy in your Xcode project root folder and make it executable (via chmod)

The file will stay untouched. You should then describe your project in the file, via the following properties:

  • sonar.projectKey/sonar.projectName
  • sonar.sources
  • sonar.objectivec.project/sonar.objectivec.workspace
  • sonar.objectivec.appScheme and sonar.objectivec.testScheme/li>

Tip: Comment sonar.objectivec.testScheme if you have no unit/UI tests in your application


After some research, I stumbled upon the iOctoCat open-source application. Despite its name, it has nothing to do with OCTO. It’s an old iOS client for GitHub (by GitHub). This application is interesting because it’s non-trivial, it has some tests (you’re not misreading) and clean dependencies (with CocoaPods).

To push things even further, here is a fork of the repository cleaned up and fixed for the latest version of Xcode.

Stage 1: look ma, no hands

Unfortunately, nothing works out-of-the-box like in Android, where you can get first analysis results with no configuration at all! But there is no free lunch, is there?

Anyway you first need to configure the demo project. This is done with the following command lines:

git clone

Tip: On a your own project, you don’t have to build the project first, the plugin takes care of that. The script is a facility provided by iOctocat developers to download and build all the dependencies of iOctocat

Stage 2: the full monty

You should configure the for this project. Here is what mine looks in the end (you can download the file from here):

# Required configuration #


# Project description
sonar.projectDescription=(Old) GitHub client for iOS

# Path to source directories (application code, not third-party code)

# Xcode project configuration
sonar.objectivec.testScheme=iOctocat Unit Tests

# Optional configuration #

# Encoding of the source code

# JUnit report generated by is stored in sonar-reports/TEST-report.xml
# Change it only if you generate the file on your own
# The XML files have to be prefixed by TEST- otherwise they are not processed 

# Paths to exclude from coverage report (tests, 3rd party libraries etc.)
# sonar.objectivec.excludedPathsFromCoverage=pattern1,pattern2

Then you run the analysis on your project with the following command line (in your Xcode project root folder):


The dashboard you get is illustrated in the following picture:

third-screenshotDashboard: iOctocat analysis

If you look at it and compare to its Android counterpart, you will see that quite a few metrics are missing, like:

  • Complexity: cyclomatic complexity by method/class/file
  • Design: dependencies between classes, files
  • Documentation: code documentation (doxygen, appledoc etc.)
  • Size: number of statements, number of classes, methods and properties
  • Tests: no support for tests other than unit tests (integration, acceptance etc.)

And then?

Your next question should be: fine it looks nice and easy but when are we going to have a full support for the Objective-C in SonarQube?

Well that’s not an easy question, as the plugin is done mostly on our free time. But it’s faster with real users: star the GitHub project and I am sure it will go faster!

Aside from this twisted answer, here is the plan as of today:

  1. have a fix release very soon for the various issues raised since last October (v0.3.2)
  2. add support for the various test frameworks out-of-there (v0.4)
  3. add a basic support for the Objective-C language (syntax highlighting, classes, methods etc.) (v0.9)

While I expect to have the first two quite soon, the latter is more my 6-12 months plan. Why so long? Because until now, the plugin was more about getting existing tools work smoothly and with no configuration. Now the hardest work begins: have SonarQube understand the Objective-C language.

Afterwards the plugin should be really useful, but still not as complete and integrated as a supported SonarQube plugin. I must say that on my side I have no plan to get it further the useful stage. But who knows, plans change!

Why did I tell you all this?

Even if the analysis is still limited, it’s nice you can get this analysis with no modification of the project at all. Personally I find it more easy to setup than its Android counterpart.

Either way, now you know how to get relevant metrics out of your neat iOS application in no time using Sonar. No more excuse for not using it on your own project, even if it is a indie iOS App.

6 commentaires sur “Track your iOS application code quality using Sonar”

  • Super Awesome Stuff !! Thanks a Lot for this Post !! Cyril
  • Guys, you may know that all these missing metrics can be acquired by OCLint , which can be extended by plugins. So, if I write these plugins, will I have that additional code metrics in Sonar? Thank you in advance.
  • is empty. Normal ?
  • Isn't it true that SonarQube Obective-C and Swift Plugins are commercial only??
  • Thanks for the detailed doc. After following all the steps, I am stuck with one step while running the demo app. Please see the output I am getting. (Tried to change the ONLY_ACTIVE_ARCH to YES in the as well as in the xcode build settings.) OUTPUT: Running .Extracting Xcode project information... .........ERROR - Command 'xctool -workspace iOctocat.xcworkspace -sdk iphonesimulator ARCHS=i386 VALID_ARCHS=i386 CURRENT_ARCH=i386 ONLY_ACTIVE_ARCH=NO -scheme iOctocat -reporter json-compilation-database:compile_commands.json build' failed with error code: 1 qbadmins-Mac-Pro-78:ioctocat qbuser$ ./ Running Extracting Xcode project information... .......ERROR - Command 'xctool -workspace iOctocat.xcworkspace -sdk iphonesimulator ARCHS=i386 VALID_ARCHS=i386 CURRENT_ARCH=i386 ONLY_ACTIVE_ARCH=NO -scheme iOctocat -reporter json-compilation-database:compile_commands.json build' failed with error code: 1
  • i have some error msg when execute the sonar-runner command: 11:52:16.717 INFO - Sensor Lines Sensor (done) | time=70ms 11:52:16.717 INFO - Sensor QProfileSensor 11:52:16.722 INFO - Sensor QProfileSensor (done) | time=5ms 11:52:16.723 INFO - Sensor ObjectiveCSquidSensor 11:52:17.575 INFO - Sensor ObjectiveCSquidSensor (done) | time=852ms 11:52:17.575 INFO - Sensor Objective-C SurefireSensor 11:52:17.575 INFO - parsing sonar-reports INFO: ------------------------------------------------------------------------ INFO: EXECUTION FAILURE INFO: ------------------------------------------------------------------------ Total time: 7.810s Final Memory: 10M/337M INFO: ------------------------------------------------------------------------ ERROR: Error during Sonar runner execution ERROR: Unable to execute Sonar ERROR: Caused by: org.sonar.api.resources.Project.getPackaging()Ljava/lang/String; ERROR: ERROR: To see the full stack trace of the errors, re-run SonarQube Runner with the -e switch.
    1. Laisser un commentaire

      Votre adresse e-mail ne sera pas publiée. Les champs obligatoires sont indiqués avec *

      Ce formulaire est protégé par Google Recaptcha