嗨,我正在尝试创建 IPSEC VPN 连接并自动连接,但即使我定义了共享密钥,它也会给出 No VPN shared secret was provided 错误。我的代码如下所示,它正在保存 VPN 配置但无法连接。
我正在尝试使用相同的参数从我的 iPhone 添加 VPN 配置并且它正在连接。
[[NEVPNManager sharedManager] setEnabled:YES];
[[NEVPNManager sharedManager] loadFromPreferencesWithCompletionHandler: ^(NSError *error) {
NEVPNProtocolIPSec *p = [[NEVPNProtocolIPSec alloc] init];
p.serverAddress =@"server";
p.authenticationMethod = NEVPNIKEAuthenticationMethodSharedSecret;
p.useExtendedAuthentication = YES;
NSString *secret = @"secretCode";
NSData *secretData = [secret dataUsingEncoding:NSUTF8StringEncoding];
p.sharedSecretReference=secretData;
p.localIdentifier=@"identifier";
p.username=@"username";
p.passwordReference=[[SSKeychain passwordForService:@"service" account:@"passwordReference"] dataUsingEncoding:NSUTF8StringEncoding];
p.disconnectOnSleep = NO;
[NEVPNManager sharedManager].protocolConfiguration=p;
NSMutableArray *rules = [[NSMutableArray alloc] init];
NEOnDemandRuleConnect *connectRule = [NEOnDemandRuleConnect new];
[rules addObject:connectRule];
[[NEVPNManager sharedManager] setOnDemandRules:rules];
[[NEVPNManager sharedManager] setLocalizedDescription:@"vpn"];
[[NEVPNManager sharedManager] setEnabled:YES];
[[NEVPNManager sharedManager] saveToPreferencesWithCompletionHandler: ^(NSError *error) {
NSLog(@"Save VPN to preference complete");
if (error) {
NSLog(@"Save error: %@", error);
}
}];
NSError *startError;
[[NEVPNManager sharedManager].connection startVPNTunnelAndReturnError:&startError];
if(startError) {
NSLog(@"Start error: %@", startError.localizedDescription);
}