Virtual Locations

Implementing Virtual Locations

When implementing virtual locations in your VPN service, you have two primary options for specifying the hop location: optimal location and custom location.

Optimal Location

The optimal location feature allows the VPN SDK to automatically select the best intermediate server for your connection. This is ideal when you want to prioritize performance and let the system handle the routing decisions.

To use the optimal location in your code, you can utilize the VirtualLocation.optimal() method. Here's an example of how to start a VPN connection with the optimal location:

import VPNApplicationSDK
// ...

let groupData = VPNGroupData(groupID: "group.com.yourcompany.vpnsdk-demo", usesSystemExtension: false)

let hydraConfiguration = HydraConfiguration(
    carrierID: "YOUR_CARRIER_ID",
    extensionBundleID: "com.yourcompany.vpnsdk-demo.neprovider",
    groupData: groupData,
    fireshieldConfig: FireshieldConfig(mode: .disabled, groupData: groupData)
)

let hydraSDK = HydraSDK(configuration: hydraConfiguration)

hydraSDK.start(location: VirtualLocation.optimal(), proxy: nil, completion: { error, credential in
    if let error = error {
        print("Failed \(error)")
    } else {
        print("Success")
    }
})

if you use location: nil, it's equivalent to location: VirtualLocation.optimal(). In both cases, the SDK will automatically select an appropriate proxy server.

Custom Location

For users who require more control over their connection routing, the custom location option allows you to specify a particular intermediate server location.

Here's an example of finding a VPN connection with a custom location:

let selectedCity = "San Jose"
hydraSDK.virtualLocations( completion: { (virtualLocationsError, result) in
    guard let virtualLocations = result else {
        print("No virtual locations available")
        return
    }

    var foundMatchingLocation = false
    for virtualLocation in virtualLocations {
        if virtualLocation.city == selectedCity {
            print("Found matching location: ", virtualLocation)
            foundMatchingLocation = true
        }
    }

    if !foundMatchingLocation {
        print("No matching location found")
    }
})

In this example, the intermediate hop is a server in San Jose, United States.

Last updated