NetworkExtension - NEVPNManager

2019-04-04 11:22发布

问题:

Apple published with iOS 8 a new Framework "NetworkExtension".

I want to start a VPN Connection out of an app with the NEVPNManager, or has this Framework another use?

Has somebody information or an example about this Framework? I can´t find information about it on the developer.apple.com website, only in the header files.

Thanks

回答1:

The code would look something like this (exact implementation depends on the type of VPN):

NEVPNManager *manager = [NEVPNManager sharedManager];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(vpnConnectionStatusChanged) name:NEVPNStatusDidChangeNotification object:nil];

NEVPNProtocolIPSec *protocol = [[NEVPNProtocolIPSec alloc] init];
protocol.username = @“[Your username]”;
protocol.passwordReference = [KeyChainAccess loadDataForServiceNamed:@“[Your Service Name]"];
protocol.serverAddress = @“[Your Server Address]“;
protocol.authenticationMethod = NEVPNIKEAuthenticationMethodCertificate;
protocol.localIdentifier = @“[Your Local identifier]”;
protocol.remoteIdentifier = @“[Your Remote identifier]”;
protocol.useExtendedAuthentication = NO;
protocol.identityData = [Your VPN certification private key];
protocol.disconnectOnSleep = NO;
[manager setProtocol:protocol];

[manager setOnDemandEnabled:NO];
[manager setLocalizedDescription:@"VPN"];
NSArray *array = [NSArray new];
[manager setOnDemandRules: array];
NSLog(@"Connection desciption: %@", manager.localizedDescription);
NSLog(@"VPN status:  %i", manager.connection.status);

[manager loadFromPreferencesWithCompletionHandler:^(NSError *error) {
    // do config stuff
    [manager saveToPreferencesWithCompletionHandler:^(NSError *error) {
    }];
}];


NSError *startError;
[[NEVPNManager sharedManager].connection startVPNTunnelAndReturnError:&startError];
if(startError) {
      NSLog(@"Start error: %@", startError.localizedDescription);
}