苹果公司在iOS 8上发布了新的框架“NetworkExtension”。
我想使用NEVPNManager在应用程序外启动VPN连接,还是该框架还有其他用途?
是否有人提供有关此框架的信息或示例?
我在developer.apple.com网站上找不到相关信息,仅在头文件中。
谢谢
最佳答案
代码看起来像这样(具体实现取决于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);
}
关于objective-c - NetworkExtension-NEVPNManager,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/24056167/