# Unified VPN SDK demo for Apple

## Prerequisites

The following prerequisites are required to use the SDK:

* iOS 18 or newer
* Latest version of Xcode (recommended)

## Setup

1. Clone app source code locally from [GitHub](https://github.com/AnchorFreePartner/vpnsdk-demo).
2. Open Xcode and create iOS application with additional Network Extension target:
   * **Application** target
   * **Network Extension** target
3. Configure both the **Application** and **Network Extension** targets:
   * Add following system frameworks to your project dependencies:
     * NetworkExtension
     * Security
     * libz.tbd
   * In your project: *Project > Build Settings*:
     * Set *Enable Bitcode* to "NO"
     * Set *Other Linker Flags* to `-ObjC`
   * Enable Network Extension and Personal VPN entitlements for both application and network extension targets
   * Create App Group that will be used on both targets
4. Install the Unified SDK:
   * Add **VPNApplicationSDK.framework** for **Application** target
   * Add **VPNTunnelProviderSDK.framework** for **Network Extension** target
5. Configure the Network Extension:

   * Make your PacketTunnelProvider class from **Network Extension** target extend AFPacketTunnelProvider:

   ```
   // PacketTunnelProvider.swift
   import VPNTunnelProviderSDK

   class PacketTunnelProvider: AFPacketTunnelProvider {}
   ```
6. Build the project.

## Usage

1. Sign in at [pango-cloud.com](https://www.pango-cloud.com).

   {% hint style="info" %} Note that at this time, it is not possible for users to create their own accounts directly if they do not already have an account established with example.com. Please contact your sales representative to initiate account creation as part of the project and client onboarding process or [contact us](https://www.pango.co/contact-us/). We apologize for any inconvenience. {% endhint %}2. In Pango dashboard, create a project and use a name for your project as a Public key. Private key is optional.
2. Navigate to the `Network` tab from the navigation bar. Then, click on the `Add location` button.
3. In Pango dashboard, navigate to the dashboard and locate your `carrier_id`.
4. You can find the backend URL for Prod/Stage/Dev [here](https://apidocs.aura-cloud.com/user.html#servers).
5. You are almost ready to connect to the VPN. To initiate the VPN connection process in the demo app:
   1. In your AppDelegate or other shared Singleton object, initialize Unified SDK:

      ```
      // AppDelegate.swift or your own Hydra holding singleton
      import VPNApplicationSDK
      // ...
      lazy var vpnClient : AFHydra = {
          AFHydra.withConfig(AFConfig.init(block: { (builder) in
              builder.baseUrl = "BASE_URL";
              builder.carrierId = "CARRIER_ID";
              builder.groupId = "group.YOUR.GROUP.NAME";
              builder.networkExtensionBundleId = "com.yourcompany.AWESOME_APP.neprovider";
          }))
      }()
      ```

      Replace placeholders with your values obtained from developer portal and your XCode or Apple Developer Portal configuration.
   2. Perform login:

      ```
      vpnClient.login(AFAuthMethod.anonymous(), completion: { [unowned self] (e, user) in
          // Process result
      })
      ```
   3. Start VPN connection:

      ```
      vpnClient.startVpn({ (country, error) in
          // VPN is being started to server in country `country`
          // Rely on notification center status notifications for exact VPN state
      })
      ```
   4. Handle VPN state notifications:

      ```
      vpnClient.notificationCenter.addObserver(forName: NSNotification.Name.AFVPNStatusDidChange, object: nil, queue: nil) { [unowned self] (notification) in
          // Process VPN status changes here
          if vpnClient.vpnStatus() == .connected {
              // Do stuff
          }
      }
      ```
6. Your device is now connected to the VPN. To verify the connection:
   * Open any browser on your device.
   * Navigate to whatsmyip.com (or any other site which can identify your IP).
   * Observe that the IP address displayed is different from your device's actual IP address, confirming that traffic is being routed through the VPN.
7. To terminate the VPN connection when finished, call the appropriate disconnect method in the SDK.

## Features usage

#### Server country selection

If you have to choose VPN server country before connecting, you can call `availableCountries` and then `startVpn` with specific country:

```
// Get a list of countries 
vpnClient.availableCountries { [weak self] (e, countries) in
    // Do stuff
    self.someCountry = countries[0]
}

// Start VPN with different country
vpnClient.startVpn(with: self.someCountry, completion: { (country, e) in

})
```

## Documentation

For more detailed information, please refer to [Unified VPN SDK for Apple](https://pango.gitbook.io/paas/sdk/unified-vpn-sdk-for-apple).
