How To Build an OpenVPN Client on iOS

Building a VPN app? Start here.

Yalçın Özdemir
Better Programming

--

VPN Software Solutions & Services For Business | OpenVPN

Most VPN apps now support OpenVPN protocol. However, when it comes to iOS, there is not much out there to help iOS developers get into it easily.

Apple realizes that there is an increasing need for VPN on mobile; they provide developers with great support built right into iOS.

Apple created the Network Extension framework to allow developers to create VPN apps easily. In this article, I will only show you how to establish a VPN connection using OpenVPN protocol. IKEv2 will be covered in another article.

Let’s start…

1. Import OpenVPNAdapter — Objective-C Wrapper for OpenVPN Library

We will use OpenVPNAdapter, which is a wrapper for OpenVPN library.

Install it using CocoaPods:

pod 'OpenVPNAdapter', :git => 'https://github.com/ss-abramchuk/OpenVPNAdapter.git', :tag => '0.4.0'

Special thanks to Sergey Abramchuk who is the creator of OpenVPNAdapter.

2. Add Required Capabilities

Apple requires apps to add Personal VPN and Network Extension capabilities if they are offering VPN services.

Click on ‘+ Capability’ button on ‘Signing & Capabilities’ to add new capabilitiy

If the capabilities don’t appear on Xcode, visit developer.apple.com and enable them for your bundle ID.

3. Add Network Extension Target

You will need to add a network extension, which you can think of as a separate app that will handle VPN operations in the background.

Here’s how Apple defines an extension: “An app extension lets you extend custom functionality and content beyond your app and make it available to users while they’re interacting with other apps or the system.”

File > New > Target

This automatically creates a new folder with your target name and a bunch of files in it. We will work with these in Step 5.

4. Configure VPN Profiles

First import NetworkExtension and start configuring your VPN profile using NETunnelProviderManager

I have provided here a very simple way of configuring NETunnelProviderProtocol(configuration parameters for a VPN tunnel.)

These parameters might change according to what your server requires.

You need to import .ovpn file into your project. This file should contain the OpenVPN directives, certificates, and keys. Basically, this file contains all the information needed to establish a VPN connection.

5. Establish a Connection

After you have created a Network Extension target, Xcode automatically generates a PacketTunnelProvider class, which is a subclass of NEPacketTunnelProvider (the principal class for a packet tunnel provider app extension.)

Here, you can configure and establish VPN connections using OpenVPNAdapter.

That’s all! Now run your app and it should establish a VPN connection.

If you are planning to display Wi-Fi SSIDs on your app, you can read my article How To Access Wi-Fi SSID on iOS 13.

--

--