使用NetworkExtension总是得到NEVPNStatusInvalid

时间:2019-12-04 16:13:38

标签: ios vpn networkextension nevpnmanager

在使用网络扩展的开发过程中,我使用NETunnelProviderManager成功创建VPN配置文件并开始连接后,状态始终为NEvpnStatusInvalid,并且在VPN配置中的设置中,我看到红色的“需要更新”配置。

- (void) startConnect {
  [NETunnelProviderManager loadAllFromPreferencesWithCompletionHandler:^(NSArray<NETunnelProviderManager *> * _Nullable managers, NSError * _Nullable error) {
    NETunnelProviderManager* tunnelProviderManager = nil;
    if (managers && managers.count > 0){
      for (NETunnelProviderManager* manager in managers) {
        if ([manager.localizedDescription isEqualToString:self.vpnLocalizedDescription]){
          tunnelProviderManager = manager;
          break;
        }
      }
    }
    if(!tunnelProviderManager) tunnelProviderManager = self.providerManager;

    NETunnelProviderProtocol* oriProtocol = (NETunnelProviderProtocol*)tunnelProviderManager.protocolConfiguration;
    oriProtocol.providerBundleIdentifier = [NSString stringWithFormat:@"%@.ssExtension", [NSBundle mainBundle].bundleIdentifier];

    tunnelProviderManager.protocolConfiguration = oriProtocol;
    [tunnelProviderManager setEnabled:YES];

    [tunnelProviderManager saveToPreferencesWithCompletionHandler:^(NSError * _Nullable error) {
      if(error == nil){
        [tunnelProviderManager loadFromPreferencesWithCompletionHandler:^(NSError * _Nullable error) {
          if(error == nil){
            NSError* connectError = nil;
            [tunnelProviderManager.connection startVPNTunnelAndReturnError:&connectError];
            NSLog(@"connectError-->%@", connectError);
          }
        }];
      }
    }];
  }];
}

0 个答案:

没有答案