# Unified VPN SDK demo for Apple

For the latest quickstart guide, see [Quickstart](/paas/getting-started/quickstart.md).

## 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).

   <div data-gb-custom-block data-tag="hint" data-style="info" class="hint hint-info"><p>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 <a href="https://www.pango.co/contact-us/">contact us</a>. We apologize for any inconvenience.</p></div>
2. In Pango dashboard, create a project and use a name for your project as a Public key. Private key is optional.
3. Navigate to the `Network` tab from the navigation bar. Then, click on the `Add location` button.
4. In Pango dashboard, navigate to the dashboard and locate your `carrier_id`.
5. You can find the backend URL for Prod/Stage/Dev [here](https://apidocs.aura-cloud.com/user.html#servers).
6. 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:

      ```
      let authMethod = AuthMethod(type: .pango, token: "YOUR_PANGO_ACCESS_TOKEN")
      vpnClient.login(authMethod, 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
          }
      }
      ```
7. 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.
8. 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

})
```

## More Information

See [Unified VPN SDK for Apple](/paas/sdk/unified-vpn-sdk-for-apple.md)

See [Troubleshooting](/paas/sdk/unified-vpn-sdk-for-apple/troubleshooting.md)


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://pango.gitbook.io/paas/sample-applications/unified-vpn-sdk-demo-for-apple.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
