Skip to content
Swift code analysis with SonarQube

11 Minutes

Swift code analysis with SonarQube

Fix Bugs Faster! Log Collection Made Easy

Get started

SonarQube allows developers to identify bugs, vulnerabilities, maintainability issues and coding standard violations using only the source code. However, it can pose specific challenges for teams that work with Swift.

In this guide we’ll show you how to overcome these limitations and use SonarQube for Swift projects in a practical, scalable way. You’ll learn:

  • What SonarQube can and cannot analyze in Swift codebases.
  • The current requirements and limitations of SonarQube (especially for iOS teams).
  • How to set up SonarQube with Xcode and run a working analysis.
  • How to read results and integrate them into CI workflows.

If you’re looking for a broader overview of how SonarQube works across languages and use cases, we cover that separately in our main SonarQube guide.

What SonarQube does for Swift code analysis

SonarQube enables us to perform Swift code analysis without running the app, helping us catch structural issues early in the development process. It focuses on code quality, not runtime behavior, and gives a centralized view of problems across the codebase.

You’ll find that it:

  • Detects bugs and potential runtime risks through static analysis.
  • Highlights code smells that reduce readability and maintainability.
  • Tracks metrics like complexity, duplication, and coverage.
  • Surfaces security issues and unsafe coding patterns.
  • Provides a web dashboard to review, prioritize, and assign issues.
  • Integrates with CI pipelines to analyze code on every build.

For Swift projects, results depend on proper setup and available support, so accuracy can vary based on the SonarQube edition and configuration.

SonarQube requirements for Swift (important limitations)

Swift support in SonarQube depends heavily on the edition used, and this is where some developers run into trouble. Keep the following points in mind:

  • Native Swift analysis requires SonarQube Developer Edition or higher.
  • Community Edition does not provide full Swift support and often depends on tools like SwiftLint.
  • A complete Xcode build is required before running analysis (SonarQube relies on compiled data).
  • SonarQube server requires Java (typically version 21 or higher).
  • SonarScanner (the CLI that analyzes source code and uploads the results to SonarQube) must be configured with correct workspace and project settings.
  • Some Swift issues may not be detected in individual cases, depending on rules, coverage, and available analyzers.

Note that running a local server does not guarantee that the Swift code analysis will work.

SonarQube Swift setup with Xcode (step by step)

Setting up SonarQube for Swift starts with choosing the right server edition, then moves into scanner setup, Xcode build preparation, and project configuration.

The order matters. A server may be running fine, but the Swift analysis will still fail if the edition or build setup is wrong.

Here’s a process that will work consistently.

  1. Set up and start the SonarQube server.
  2. Install SonarScanner.
  3. Build the Xcode project to generate analysis data.
  4. Configure the sonar-project.properties file.
  5. Run SonarScanner and send the analysis to SonarQube.

Each step depends on the previous one, so keeping the sequence clean helps avoid the classic “server works, but Swift does not” problem.

Step 1: Run SonarQube server (Docker or manual setup)

We start by running a local SonarQube server. Analysis results will be sent and reviewed here.

  • Option 1: Docker (recommended if already installed)
    • Install Docker Desktop and make sure it’s running.
    • Pull and run SonarQube: docker run -d -p 9000:9000 --name sonarqube sonarqube:latest
    • Wait a few minutes for initialization.
  • Option 2: Manual installation (more predictable setup)
    • Download SonarQube from the official website.
    • Extract and move it to /Applications .
    • Install Java (required) in terminal with Homebrew: brew install openjdk@21
    • Start the server: sh /Applications/sonarqube/bin/macosx-universal-64/sonar.sh console

Once running, open http://localhost:9000 and log in (admin/admin).

At this point, we only need the server up and accessible.

Step 2: Install SonarScanner

SonarScanner is the CLI tool that sends analysis results from the Swift project to the SonarQube server. It must be installed and available in the terminal before you run any analysis.

  • Install via Homebrew (recommended on macOS): brew install sonar-scanner .
  • Verify installation: sonar-scanner -v .
  • Ensure it’s available in the system PATH.
  • If needed, download manually from the official website and add it to PATH.

SonarScanner will be used later to trigger the analysis and upload results to the SonarQube dashboard.

Step 3: Prepare your Xcode project build

Before running SonarQube Swift analysis, we need a successful Xcode build to generate the required data.

SonarQube requires data from a successful Xcode build to analyze Swift code. Without this step, the analysis may return incomplete or empty results.

  • Open the project or workspace in Xcode.
  • Select any valid iOS simulator (latest available is fine).
  • Clean the build folder: Shift + Cmd + K .
  • Build the project: Cmd + B .
  • Ensure the build completes without errors.

This step generates the information SonarQube needs to understand the structure and behavior of the Swift code before scanning.

Step 4: Configure sonar-project.properties for Swift

SonarQube uses a specific configuration file to understand how to analyze the Swift project. This file defines project details, source paths, and Swift-specific settings.

  • Create a file named sonar-project.properties in the project root.
  • Add basic project information:sonar.projectKey, sonar.projectName, sonar.projectVersion .
  • Define source and exclusions:sonar.sources=.sonar.exclusions=**/Pods/** .
  • Add Swift settings:sonar.swift.workspace and sonar.swift.project .

Make sure all values match the actual Xcode workspace and project names. Any misalignment here can impact the quality of the results.

Step 5: Run SonarScanner and analyze Swift code

With the project built and configured, we can now run SonarScanner to analyze the Swift code and send results to the SonarQube server.

  • Open Terminal and navigate to the project root.
  • Run the scanner: sonar-scanner .
  • Wait for the analysis to complete.
  • Check the output for errors or warnings.

Once finished, open http://localhost:9000 and navigate to the project dashboard to review the results.

How to read SonarQube Swift analysis results

After analysis, SonarQube shows issues by type and severity. It does not group them by priority. However, we can use the following metrics to decide what to fix first based on impact.

Metric (priority)What it means
Bugs (High priority – fix first)Issues that can cause crashes or incorrect behavior
Vulnerabilities (High priority – fix first)Security issues that could be exploited
Code Smells (Medium priority)Maintainability problems that make code harder to work with
Complexity (Medium priority)Code that is harder to understand and more error-prone
Coverage (Lower priority)Percentage of code covered by tests
Duplications (Lower priority)Repeated code that increases maintenance effort

This helps prioritize critical issues first, then focus on long-term improvements.

SwiftLint for additional linting

SwiftLint adds style and convention checks on top of SonarQube, helping catch issues earlier in local development. It is not required for SonarQube, but it improves consistency and enforces team standards.

  • Install SwiftLint with Homebrew: brew install swiftlint .
  • Verify installation: swiftlint version .
  • Run it in the project folder: swiftlint .

You can customize the setup by creating a .swiftlint.yml file in the project root. This allows you to enable, disable, or tune specific rules based on project needs (see https://github.com/realm/SwiftLint#configuration).

Optionally, you can add SwiftLint as a Run Script phase in Xcode to check code automatically on every build.

Used this way, SwiftLint complements SonarQube by catching style and linting issues earlier in the workflow.

SonarQube vs SwiftLint for Swift code quality

SonarQube and SwiftLint serve different roles in Swift code analysis for iOS projects. We use them together rather than choosing one over the other.

SonarQubeSwiftLint
Detects bugs, vulnerabilities, and code smellsEnforces style, formatting, and naming rules
Provides project-wide metrics (coverage, complexity, duplication)Focuses on file-level linting and conventions
Runs after builds, often in CI/CD pipelinesRuns locally or during builds for fast feedback
Requires server setup and configurationSimple setup via Homebrew
Slower, deeper analysisFast, immediate feedback

In practice:

  • Use SwiftLint during development for quick feedback
  • Use SonarQube for deeper analysis and overall code quality insights

Together, they provide a complete Swift code quality workflow.

SonarQube CI/CD integration for Swift projects

In SonarQube iOS workflows, CI/CD integration ensures analysis runs automatically on every build. This is another major check in the box for SonarQube and a major advantage when flagging issues.

Here are some best-practice tips to optimize the integration:

  • Run sonar-scanner as part of the CI pipeline after the Xcode build step.
  • Use tools like GitHub Actions, Jenkins, or Bitrise for iOS workflows.
  • Pass authentication token securely via environment variables.
  • Ensure the build step runs successfully before triggering analysis.

This setup turns code quality checks into a continuous process instead of a manual task.

GitHub Actions example

We can automate SonarQube analysis in GitHub Actions by running the scanner after building the Swift project. The build step is required so SonarQube can analyze compiled data correctly.

  • Create a workflow file at .github/workflows/sonarqube.yml .
  • Add a job that builds the Xcode project and runs the scanner.
  • Store the SonarQube token in GitHub Secrets.

Example:

name: SonarQube Analysis

on:
  push:
    branches: [ main ]

jobs:
  sonar:
    runs-on: macos-latest

    steps:
      - uses: actions/checkout@v3

      - name: Build project
        run: xcodebuild clean build \
          -workspace YourWorkspace.xcworkspace \
          -scheme YourScheme \
          -destination 'platform=iOS Simulator,name=iPhone 15'

      - name: Run SonarScanner
        run: sonar-scanner
        env:
          SONAR_TOKEN: ${{ secrets.SONAR_TOKEN }}

This setup ensures Swift code is analyzed automatically on every push, keeping code quality checks consistent across the team.

Jenkins or Bitrise overview

The open-source Jenkins server and the Cloud-based Bitrise platform both enable us to integrate SonarQube into CI pipelines. Jenkins integrates SonarQube as a pipeline plugin, while Bitrise integrates it as a workflow step.

The initial part of the integration is the same in both cases:

  • Run the Xcode build step before triggering sonar-scanner
  • Store the SonarQube token securely as an environment variable
  • Add a dedicated step in the pipeline for analysis

In Jenkins, we typically install the SonarQube plugin and configure it under system settings. For a deeper overview, see our guide: https://bugfender.com/blog/what-is-jenkins-and-why-should-you-be-using-it/

In Bitrise, we add steps for Xcode build and then run SonarScanner as a script step.

Common SonarQube Swift issues and fixes

When working with SonarQube in Swift and Xcode projects, issues often surface around setup and Xcode builds. Most problems come from missing build data or incorrect configuration.

IssueFix
No Swift files detectedEnsure the Xcode project is built successfully before running sonar-scanner
Analysis runs but shows 0 issuesCheck sonar.sources and project paths in sonar-project.properties
Scanner fails with authentication errorVerify SONAR_TOKEN is set correctly in environment variables
Build succeeds but analysis is emptyConfirm workspace and scheme are correctly defined
Pods or generated files includedExclude them using sonar.exclusions=**/Pods/**
Java errors when starting SonarQubeInstall a supported Java version (e.g. brew install openjdk@21)
Scanner command not foundAdd SonarScanner to PATH or install it properly
Results not updating in dashboardEnsure the project key matches and analysis is sent to the correct server

These fixes usually resolve most Swift-related SonarQube issues without deeper debugging.

Summary: SonarQube for Swift in practice

Setting up SonarQube for Swift is less about installation and more about getting the flow right. Once your server, build, and scanner are aligned, analysis becomes reliable and repeatable.

Static analysis helps prevent issues early, but it does not replace runtime visibility. Problems that only appear on real devices, async flows, or production environments still require logging and monitoring.

This is where combining approaches makes sense.

If we want to go beyond static analysis and understand what actually happens in production, we can try Bugfender for free.

Used together, static analysis and real-time logging give us a more complete view of code quality and behavior.

SonarQube Swift FAQ

Does SonarQube work with Swift in the Community Edition?

Not fully. Community Edition does not provide native Swift analysis, so results may be incomplete or rely on external tools like SwiftLint.

For full Swift support, we need SonarQube Developer Edition or higher.

Why does SonarQube show no issues after running analysis?

This usually happens when the Xcode build step was skipped or failed.

SonarQube depends on compiled data, so without a successful build, it may scan files but return empty or misleading results.

Do we need SwiftLint if we already use SonarQube?

Not strictly, but it helps.

SwiftLint provides fast, local feedback on style and conventions, while SonarQube focuses on deeper analysis and project-wide insights.

Using both improves coverage.

Can SonarQube detect runtime crashes in Swift apps?

No. SonarQube is a static analysis tool.

It analyzes code structure and patterns, but it does not execute code or capture runtime errors. For production issues, logging and monitoring tools are required.

Why does SonarQube require an Xcode build for Swift?

Swift analysis relies on build artifacts to understand the project structure.

Without building the project, SonarQube cannot accurately analyze dependencies, modules, or compiled code paths.

Can we run SonarQube without CI/CD?

Yes. We can run SonarScanner locally from the terminal.

CI/CD integration is optional, but recommended for automating analysis and keeping code quality consistent across the team.

How long does SonarQube analysis take for Swift projects?

It depends on project size and configuration.

  • Small projects: seconds to a few minutes
  • Larger iOS apps: several minutes

Initial runs are usually slower, while incremental analyses are faster.

Is SonarQube enough for debugging Swift apps?

No. SonarQube helps prevent issues before runtime, but it does not provide visibility into production behavior.

For real-world debugging, especially on user devices, we need runtime logging and monitoring alongside static analysis.

Expect The Unexpected!

Debug Faster With Bugfender

Start for Free
blog author

Aleix Ventayol

Aleix Ventayol is CEO and co-founder of Bugfender, with 20 years' experience building apps and solutions for clients like AVG, Qustodio, Primavera Sound and Levi's. As a former CTO and full-stack developer, Aleix is passionate about building tools that solve the real problems of app development and help teams build better software.

Join thousands of developers
and start fixing bugs faster than ever.