NEVPNProtocolIPSec unavailable on Apple TV?

74 views Asked by At

I'm porting my VPN app to tvOS 17. In iOS the VPN connects successfully, however in tvOS saveToPreferences returns error Code 1: Could not save VPN Configurations: Missing protocol or protocol has invalid type

Could you advise what's the problem with my code?

 func toggleVPN() {
    let vpnManager = NEVPNManager.shared()

    vpnManager.loadFromPreferences { (error) in
      if let error = error {
        print("Could not load VPN Configurations: \(error.localizedDescription)")
        return
      }

      if vpnManager.connection.status == .connected || vpnManager.connection.status == .connecting {
        vpnManager.connection.stopVPNTunnel()
      } else {
        vpnManager.isEnabled = true
        vpnManager.isOnDemandEnabled = true
        vpnManager.localizedDescription = "tvpn"
        let p = NEVPNProtocolIPSec()
        p.authenticationMethod = .sharedSecret // or .certificate
        p.serverAddress = <REDACTED>
        p.username = "client"
        p.useExtendedAuthentication = true
        // Retrieve password and shared secret references from the keychain

        let secretData = <REDACTED>.data(using: .utf8)!
        let passData = <REDACTED>.data(using: .utf8)!
        p.sharedSecretReference = try! VPNKeychain.persistentReferenceFor(service: "vpn", account: "SharedSecret", password: secretData) // I took this part from Apple dev forums, it's tested in iOS
        p.passwordReference = try! VPNKeychain.persistentReferenceFor(service: "vpn", account: "Password", password: passData)

        vpnManager.protocolConfiguration = p
        vpnManager.saveToPreferences { (error) in
          if let error = error {
            print("Could not save VPN Configurations: \(error.localizedDescription)")
            return
          }

          do {
            try vpnManager.connection.startVPNTunnel()
          } catch {
            print("Could not start VPN Connection: \(error.localizedDescription)")
          }
        }
      }
    }
  }

persistentReferenceFor implementation can be found here: https://developer.apple.com/forums/thread/84194?answerId=252309022#252309022

This code without any changes works on iOS

Another thing I noticed, is that there's no com.apple.developer.networking.vpn.api entitlement for tvOS 17, but NEVPNManager is available there according to documentation.

0

There are 0 answers