Use the NetworkExtension library to configure VPN and networkextensionvpn

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.