title: Vpn on demand in ios 9.2
body: <p>I want to use <code>VPN on Demand</code> feature that is available in <code>Network Extension framework</code>,I want to connect the VPN when the specific URL or site is open in the browser.</p>
  
  <p>For Example: when I write <code>www.google.com</code> my VPN will be connected and not on any other sites.</p>
  
  <p>After a lot of searching on internet,I tried the following code</p>
  
  <pre><code>NEVPNManager *manager = [NEVPNManager sharedManager];
   [manager loadFromPreferencesWithCompletionHandler:^(NSError *error) {
              if(error) {
                  NSLog(@"Load error: %@", error);
              } else {
  
  
  NEVPNProtocolIKEv2 *p = [[NEVPNProtocolIKEv2 alloc] init];
     p.username = @"Username";
     p.passwordReference = [res objectForKey:@"v_PersistentRef"];
     p.serverAddress = strAddress;
     p.authenticationMethod = NEVPNIKEAuthenticationMethodCertificate;
     p.serverCertificateIssuerCommonName = @"COMODO RSA Domain Validation Secure Server CA";
     p.serverCertificateCommonName =strAddress;
     p.identityData = [NSData dataWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"point-to-client2" ofType:@"p12"]];
     p.identityDataPassword = @"vpnuser";
     p.localIdentifier = strAddress;
     p.remoteIdentifier = strAddress;
     p.useExtendedAuthentication = YES;
     p.disconnectOnSleep = NO;
     [manager setProtocol:p];
  
     NSLog(@"password: %@", [manager protocol].passwordReference);
     [manager setOnDemandEnabled:YES];
     [manager setEnabled:YES];
  </code></pre>
  
  <p>Then setting the onDemandRules by the following code:</p>
  
  <pre><code> NEEvaluateConnectionRule * ru = [[NEEvaluateConnectionRule alloc] initWithMatchDomains:@[@"google.com"]  andAction:NEEvaluateConnectionRuleActionConnectIfNeeded];
  
  
  ru.probeURL = [[NSURL alloc] initWithString:@"http://www.google.com"];
  
  
  NSArray *arr = [[NSArray alloc] initWithObjects:ru, nil];
   NEOnDemandRuleEvaluateConnection *ec =[[NEOnDemandRuleEvaluateConnection alloc] init];
  ec.interfaceTypeMatch = 2;
  [ec setConnectionRules:arr];
   NSArray *arr2 = [[NSArray alloc] initWithObjects:ec, nil];
              NSLog(@"onDemandRules: %@", arr2);
  //
              [manager setOnDemandRules:arr2];
              [manager setLocalizedDescription:@"VPN Profile"];
              [manager saveToPreferencesWithCompletionHandler:^(NSError *error) {
                  if(error) {
                      NSLog(@"Save error: %@", error);
                  }
                  else {
                   NSLog(@"Saved");
                  }
  </code></pre>
  
  <p>Here the VPN configuration profile is updated and then start VPN connection through the following code:</p>
  
  <pre><code>NSError *startError;
      [[NEVPNManager sharedManager].connection startVPNTunnelAndReturnError:&amp;startError];
      if(startError) {
          NSLog(@"Start error: %@", startError.localizedDescription);
      } else {
          NSLog(@"Connection established!");
      }
  </code></pre>
  
  <p>Now I faced the following problems:</p>
  
  <p>1)When the profile is updated the ondemand feature is not working as when I write in the browser Url i.e www.google.com,the vpn is not connected.So I cannot understand what I did wrong in the code ?</p>
  
  <p>2)How to give the dynamic domain or url like what I placed in the <code>initWithMatchDomains:@[@"google.com"]</code> or in the <code>probeUrl</code> that will be workable for google.com as well as google.de or any google domain??</p>
  
  <p>I know its quite a long and detailed question but I really want help.</p>
  
  <p>Any help will be highly appreciated.</p>
  
  <p>Thanks</p>