Hey to all
it's my first question here and I wish it helps me.
I want to create an IKEv2 VPN connection with swift, I tried to find a sample or a place for learning how to do it , but I can't find anything (I watched WWDC15 , but it doesn't work on swift 4.2 or more)
However I tried to create it with StackOverFlow's issues. but there is a bunch of question
- How can I prepare my cert Key for put it on NEVPNProtocolIKEv2() variable??? ( is .data(using: String.Encoding.utf8) enough???)
- Does cert's string need -----BEGIN CERTIFICATE----- and -----END CERTIFICATE----- ???
- How can I prepare my password like first question???
and finally here is my code, But it doesn't work 😟 :
import NetworkExtension
import UIKit
public final class IKEV2Connection {
public func initProfile() {
let vpnManager = NEVPNManager.shared()
let server = "My server IP"
let userName = "VPNUserName"
let remoteIdentifier = server
let cert = """
-----BEGIN CERTIFICATE-----
MIIFQjCCAyqgAwIBAgIIeB6GOr0OS80wDQYJKoZIhvcNAQEMBQAwPzELMAkGA1UE
......
U4Yqeart5YXLigwZ2cPOATEmv6SNIlp68eUVsPg4gvdYbe6Or0kBiScCKcH3
shfuLOjSTEIcedpQEy7b
-----END CERTIFICATE-----
""".data(using: .utf8)
vpnManager.loadFromPreferences { (error) in
if error != nil {
print("Load config faild : \(error!.localizedDescription)")
return
}
let weakP = vpnManager.protocolConfiguration as? NEVPNProtocolIKEv2
var p: NEVPNProtocolIKEv2!
if weakP != nil {
p = weakP!
} else {
p = NEVPNProtocolIKEv2()
}
p.username = userName
p.serverAddress = server
p.certificateType = .RSA
p.passwordReference = "VPNPassword".data(using: .utf8)
p.sharedSecretReference = cert
p.authenticationMethod = NEVPNIKEAuthenticationMethod.sharedSecret
p.remoteIdentifier = remoteIdentifier
p.useExtendedAuthentication = true
p.disconnectOnSleep = true
vpnManager.protocolConfiguration = p
vpnManager.localizedDescription = "IPSec IKEv2 Demo3"
vpnManager.isEnabled = true
vpnManager.saveToPreferences(completionHandler: { (error) in
if error != nil {
print("Save config failed : \(error!.localizedDescription)")
}
})
do {
try vpnManager.connection.startVPNTunnel()
} catch let ex {
print("error is \(ex)")
}
}
}
}