iOS Development on VSCode

Foti Dim
The Startup
Published in
4 min readJul 26, 2020

Using an IDE other than Xcode for iOS development until recently was a rather lonely path. This however tends to change with recent developments in the Apple ecosystem. Swift runs officially on Linux, Windows, AWS lambda, docker, and other places where you wouldn’t expect to find a programming language made by Apple.

Apple also wants to enable Swift to be a first-class citizen in 3rd party IDEs providing error reporting and code auto-completion. For that reason, they are developing SourceKit-LSP with plugins ready for popular IDEs such as Visual Studio Code (aka VSCode).

Why would you want to use an IDE other than Xcode? Well, Xcode is not available on Linux or Windows in the first place. But even when being on a Mac, VSCode offers several nice features among of them a killer one called Live Share that allows 2 or more developers to collaborate remotely on the same file with live code co-editing. You can think of it as Google Docs for code.

Live Share in action. The cursor belongs to the other participant.

In the days of post-Covid and home office this feature is a real life saver and makes a developer’s life so much easier either if you want to do pair programming or briefly explain and get assistance on a certain task.

If you try to use Swift in VSCode you will get something like that:

Auto-completion does not work as expected

The steps needed to enable VSCode for iOS development are basically 3:

  • Enable Swift language support
  • Add iOS framework support
  • Add “app” target support

Since this guide focuses on iOS and this is currently possible only with a Mac for the rest of the guide we will assume we are running on a Mac. Otherwise you can go as far a step 1.

Enable Swift language support

Make sure you have Xcode and VSCode installed. This is the easiest way to hit the ground running since sourcekit-lsp comes prebundled with Xcode. Open a terminal and run:

$ xcrun sourcekit-lsp

Assuming that you don’t see any output this is normal and means it works as expected. Hit Ctrl+C to end the process.

Build the SourceKit-LSP extension

Now it is time to build the SourceKit-LSP extension. There is an unofficial SourceKit-LSP — Unofficial CI build but this did not work for me. Building it from source is not that hard. Just make sure you have node.js installed and then run:

$ git clone https://github.com/apple/sourcekit-lsp.git
$ cd sourcekit-lsp/Editors/vscode/
$ npm run createDevPackage
$ code --install-extension out/sourcekit-lsp-vscode-dev.vsix

After that restart VSCode.

Contrary to what you might read elsewhere you don’t need to edit the source code of the extension at all.

If you try to open a Swift file now you will get an error

The plugin can’t locate SourceKit-LSP’s location

Configure the extension

You need to let the plugin know the location of the sourcekit-lsp executable. Open settings.json from VSCode using Cmd+Shift+P and enter “Preferences: Open Settings (JSON)”.

Insert this JSON as the last entry in the existing JSON. Remember to add a comma in the previous line.

"sourcekit-lsp.serverPath": "/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/sourcekit-lsp"

Add iOS framework support

What is we achieved so far is to enable Swift development but VSCode has no idea about iOS frameworks. If you try to import UIKit you will get an error.

The UIKit framework is not recognized

Insert this JSON in settings.json. You might need to update the x86_64-apple-ios13.6-simulator line based on the current version that you have installed.

"sourcekit-lsp.serverArguments": ["-Xswiftc","-sdk","-Xswiftc","/Applications/Xcode.app/Contents/Developer/Platforms/iPhoneSimulator.platform/Developer/SDKs/iPhoneSimulator.sdk","-Xswiftc","-target","-Xswiftc","x86_64-apple-ios13.6-simulator",],

This will work for Swift packages that reference iOS frameworks like UIKit.

Add “app” target support

So far we added iOS support but only for Swift packages. What if you want to work on a typical app target. Trying to open the root folder gives this error:

SourceKit-LSP is looking for a Swift package manifest file

What if you want to work on an iOS app and not on a Swift Package? SourceKit-LSP always looks for a package.json in root folder assuming that it always deals with a Swift package. An iOS app is not a Swift package and for that reason we need to trick SourceKit-LSP by adding a dummy Package.swift. Add this file to the root of your project (same level as .xcodeproj)

// swift-tools-version:5.2
import PackageDescription
let packageName = "PROJECT_NAME" // <-- Change this to yourslet package = Package(
name: "",
// platforms: [.iOS("9.0")],
products: [
.library(name: packageName, targets: [packageName])
],
targets: [
.target(
name: packageName,
path: packageName
)
]
)

Don’t forget to set packageName to your own one.

Double check the location of the file

Restart VSCode one last time and voila! Proper UIKit auto-completion

UIKit auto-completion is working 🎉

--

--

Foti Dim
The Startup

Software craftsman — iOS📱 MachineLearning🤖 AutonomousDriving🚙 AR👓 Bass🎸 Nintendo🎮