简体   繁体   English

在网络从wifi切换到移动数据期间,VPN正在停止。 在iOS中以编程方式连接了VPN

[英]VPN is stopping during network is switching from wifi to mobile data. VPN is connected programmatically in iOS

I am using Network Extension framework for configure and connect VPN server programmatically. 我正在使用网络扩展框架以编程方式配置和连接VPN服务器。 I can start and stop VPN. 我可以启动和停止VPN。 I have written following code to configure VPN in viewDidLoad. 我编写了以下代码以在viewDidLoad中配置VPN。

NEVPNManager *manager = [NEVPNManager sharedManager];
[manager loadFromPreferencesWithCompletionHandler:^(NSError *error) {

        if(manager.protocol == nil)
        {
            NSString *filePath = [[NSBundle bundleForClass:[self class]] pathForResource:@"VPNCert" ofType:@"p12"];
            NSData *certData = [NSData dataWithContentsOfFile:filePath];
            NSString *certPassword = @"password";

            NSString *vpnUsername = @"username";
            NSString *vpnPassword = @"password";
            NSString *vpnUrl = @"VPN Server IP";

            // This saves my credentials to the keychain and returns a persistent keychain reference
            NSData *passRef = [self addVPNCredentialsToKeychain:vpnUsername withPassword:vpnPassword];

            NEVPNProtocolIPSec *p = [[NEVPNProtocolIPSec alloc] init];
            p.authenticationMethod = NEVPNIKEAuthenticationMethodCertificate;
            p.serverAddress = vpnUrl;
            p.username = vpnUsername;
            p.passwordReference = passRef;
            p.identityData = certData;
            p.identityDataPassword = certPassword;
            p.disconnectOnSleep = NO;
            p.useExtendedAuthentication = YES;

            manager.protocol = p;
            manager.enabled = YES;
            [manager setOnDemandEnabled:NO];
            [manager setLocalizedDescription:@"VPN Network"];
            [manager saveToPreferencesWithCompletionHandler:^(NSError *error) {

                if(error)
                {
                    NSLog(@"Load error: %@", error);
                }
            }];
        }
    }];

I also wrote code for start VPN when button is pressed. 当按下按钮时,我还编写了启动VPN的代码。

- (IBAction)buttonPressed:(id)sender {

    NEVPNManager *manager = [NEVPNManager sharedManager];
    [manager loadFromPreferencesWithCompletionHandler:^(NSError *error) {

                if (!error)
                {
                    NSError *startError = [[NSError alloc] init];
                    [[NEVPNManager sharedManager].connection startVPNTunnelAndReturnError:&startError];
                }
            }];
}

Cases working for me. 案件为我工作。

  1. If i connect VPN in mobile data and switched to wifi this case VPN is not disconnecting. 如果我将VPN连接到移动数据中并切换到wifi,则这种情况下VPN不会断开连接。
  2. Also working if i connect VPN in mobile data and switched to wifi and then back to mobile data VPN is not disconnecting. 如果我在移动数据中连接VPN并切换到wifi,然后再回到移动数据,VPN也不会断开,则也可以正常工作。

Problem is 问题是

If i connect VPN in wifi and then switch to mobile data, in this case VPN is stopping. 如果我在wifi中连接VPN,然后切换到移动数据,则VPN正在停止。 I want VPN must stop only when user will stop the VPN. 我希望仅当用户停止VPN时,VPN才必须停止。

Is any steps are missing while configuring VPN because of that VPN is stopping? 由于VPN正在停止,在配置VPN时是否缺少任何步骤?

Thanks in Advance. 提前致谢。

You would need to add rules to keep the connection persisted. 您将需要添加规则以保持连接持久。 Also enable onDemand [manager setOnDemandEnabled:YES]; 还启用onDemand [manager setOnDemandEnabled:YES];

Swift 迅速

    let connectRule = NEOnDemandRuleConnect()
    connectRule.interfaceTypeMatch = .any

    let disconnectRule = NEOnDemandRuleDisconnect()
    disconnectRule.probeURL = URL(string:VPNCredentialsModel.instance.vpnProbeURL()!)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM