I'm porting my VPN app to tvOS 17. Here's some code which works just fine in iOS but doesn't in tvOS 17:
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)")
}
}
}
}
}
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
Another thing I noticed, that there's no com.apple.developer.networking.vpn.api entitlement for tvOS, but NEVPNManager is available there according to documentation.
Could you advise what's the problem with my code?