NetworkExtension - NEVPNManager

本小妞迷上赌 提交于 2019-12-05 16:29:36

问题


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);
}


来源:https://stackoverflow.com/questions/24056167/networkextension-nevpnmanager

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!