Use the NetworkExtension library to configure VPN and networkextensionvpn
VPN is simply a channel connecting to the LAN. After Ios8, Apple added a VPN interface NEVPNManager, which can easily add VPN connections.
First in your Xcode, go to TARGETS-> Capabilities-> open persion VPN
Add the NetWorkExtension Library to the Project
After the header file <NetworkExtension/NEVPNManager. h> is introduced
// Create a management object
NEVPNManager * vpnManager = [NEVPNManager sharedManager];
Load the VPN from the settings
// Configure the VPN
[Manager loadFromPreferencesWithCompletionHandler: ^ (NSError * _ Nullable error ){
NEVPNProtocolIPSec * set = (NEVPNProtocolIPSec *) self. manager. protocolConfiguration;
If (! Set)
{
Set = [[NEVPNProtocolIPSec alloc] init];
}
Set. username = @ "username"; // VPN username
Set. passwordReference = [@ "password" dataUsingEncoding: NSUTF8StringEncoding]; // VPN password
Set. serverAddress = @ "ip"; // ip address
Set. sharedSecretReference = [@ "SecretReference" dataUsingEncoding: NSUTF8StringEncoding];
// NEVPNIKEAuthenticationMethodCertificate: uses the certificate and private key as the authentication credential.
// NEVPNIKEAuthenticationMethodSharedSecret: used to authenticate creden
Set. authenticationMethod = NEVPNIKEAuthenticationMethodSharedSecret;
Set. useExtendedAuthentication = YES; // This is a flag indicating that extension verification will be negotiated if
Set. disconnectOnSleep = NO; // This Boolean indicates whether the device must be disconnected to sleep during VPN connection.
Self. manager. protocolConfiguration = set;
Self. manager. localizedDescription = @ "hrjd"; // description of the VPN
[Self. manager setOnDemandEnabled: YES];
[Self. manager saveToPreferencesWithCompletionHandler: ^ (NSError * _ Nullable error ){
NSError * error1;
// Connect to the VPN
[Manager. connection startVPNTunnelAndReturnError: & error1];
If (error ){
NSLog (@ "VPN connection failed ");
} Else {
NSLog (@ "VPN connection successful ");
}
}];
}];
[VpnManager. connection stopVPNTunnel];
// Disconnect the VPN