Hi Everyone,I'm hoping someone can help me. I'm working on an interal project for my company when the people in the building use the app on the WiFi.We want to add the ability for the commands send over the network to be sent via a VPN connection when the users device disconnects from the WiFi for any reason.This is my code currently but I am not having any luck.Creating the VPN Connection.NEVPNManager *manager = [NEVPNManager sharedManager];
[manager loadFromPreferencesWithCompletionHandler:^(NSError *error) {
if(error) {
NSMutableString *tempyMessage = [[NSMutableString alloc] init];
[tempyMessage appendString:@"There was an error with NEVPNManager\n\n"];
[tempyMessage appendString:[error localizedDescription]];
NSLog(@"Load error: %@", error);
[[[UIAlertView alloc] initWithTitle:@"VPN Check" message:tempyMessage delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil] show];
} else {
// No errors! The rest of your codes goes here...
[[[UIAlertView alloc] initWithTitle:@"VPN Check" message:@"No error" delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil] show];
NSString *username = @"user";
NSString *password = @"password";
[KeyChainManager saveButtonPressed:username password:password];
NSString *secret = @"secret";
NSString *secretString = @"unique-secret";
[KeyChainManager saveButtonPressed:secretString password:secret];
NEVPNProtocolIPSec *p = [[NEVPNProtocolIPSec alloc] init];
p.username = username;
p.passwordReference = [KeyChainManager getPasswordData:username];
p.serverAddress = @"vpn_server";
p.authenticationMethod = NEVPNIKEAuthenticationMethodSharedSecret;
p.sharedSecretReference = [KeyChainManager getPasswordData:secretString];
p.localIdentifier = @"vpn";
p.remoteIdentifier = @"vpn";
p.useExtendedAuthentication = NO;
p.disconnectOnSleep = YES;
[manager setProtocol:p];
[manager setOnDemandEnabled:p];
[manager setLocalizedDescription:@"My_App_VPN"];
[manager saveToPreferencesWithCompletionHandler:^(NSError *error2){
if(error2) {
NSMutableString *tempyMessage = [[NSMutableString alloc] init];
[tempyMessage appendString:@"There was an error with NEVPNManager\n\n"];
[tempyMessage appendString:[error2 localizedDescription]];
NSLog(@"Load error: %@", error2);
[[[UIAlertView alloc] initWithTitle:@"Saving VPN..." message:tempyMessage delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil] show];
} else {
// No errors! The rest of your codes goes here...
[[[UIAlertView alloc] initWithTitle:@"Saving VPN..." message:@"Saved" delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil] show];
}
}];
}
}];
manager = nil;Code calling the VPN connection.[[NEVPNManager sharedManager] setEnabled:true];
[[NEVPNManager sharedManager] setOnDemandEnabled:true];
NSError *startError;
[[NEVPNManager sharedManager].connection startVPNTunnelAndReturnError:&startError];
if(startError){
NSMutableString *tempyMessage = [[NSMutableString alloc] init];
[tempyMessage appendString:@"There was an error with NEVPNManager\n\n"];
[tempyMessage appendString:[startError localizedDescription]];
NSLog(@"Load error: %@", startError);
[[[UIAlertView alloc] initWithTitle:@"No Connection" message:tempyMessage delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil] show];
}
else{
//Network call is made here.
/*
This is where the call to the server is not being processed either by not being sent to the server,
as the server can only be access via VPN or due to issues in the above code.
*/
}Hopefully someone can help me or point me in the right direction.