I am porting my VPN app to tvOS 17. In iOS the VPN connects efficiently, nonetheless in tvOS saveToPreferences returns error Code 1: Couldn’t save VPN Configurations: Lacking protocol or protocol has invalid kind
May you advise what’s the issue with my code?
func toggleVPN() {
let vpnManager = NEVPNManager.shared()
vpnManager.loadFromPreferences { (error) in
if let error = error {
print("Couldn't load VPN Configurations: (error.localizedDescription)")
return
}
if vpnManager.connection.standing == .related || vpnManager.connection.standing == .connecting {
vpnManager.connection.stopVPNTunnel()
} else {
vpnManager.isEnabled = true
vpnManager.isOnDemandEnabled = true
vpnManager.localizedDescription = "tvpn"
let p = NEVPNProtocolIPSec()
p.authenticationMethod = .sharedSecret // or .certificates
p.serverAddress = <REDACTED>
p.username = "consumer"
p.useExtendedAuthentication = true
// Retrieve password and shared secret references from the keychain
let secretData = <REDACTED>.information(utilizing: .utf8)!
let passData = <REDACTED>.information(utilizing: .utf8)!
p.sharedSecretReference = strive! VPNKeychain.persistentReferenceFor(service: "vpn", account: "SharedSecret", password: secretData) // I took this half from Apple dev boards, it is examined in iOS
p.passwordReference = strive! VPNKeychain.persistentReferenceFor(service: "vpn", account: "Password", password: passData)
vpnManager.protocolConfiguration = p
vpnManager.saveToPreferences { (error) in
if let error = error {
print("Couldn't save VPN Configurations: (error.localizedDescription)")
return
}
do {
strive vpnManager.connection.startVPNTunnel()
} catch {
print("Couldn't begin VPN Connection: (error.localizedDescription)")
}
}
}
}
}
persistentReferenceFor
implementation will be discovered right here: https://developer.apple.com/boards/thread/84194?answerId=252309022#252309022
This code with none adjustments works on iOS
One other factor I seen, is that there is not any com.apple.developer.networking.vpn.api entitlement for tvOS 17, however NEVPNManager is obtainable there based on documentation.