Reputation: 21
The VPN connect is not Working from code. Why is not connected to the server? The code is:
- (void)viewDidLoad {
[super viewDidLoad];
// init VPN manager
self.vpnManager = [NEVPNManager sharedManager];
// load config from perference
[vpnManager loadFromPreferencesWithCompletionHandler:^(NSError *error) {
if (error) {
NSLog(@"Load config failed [%@]", error.localizedDescription);
return;
}
if (vpnManager.protocol) {
// config exists
}
// config IPSec protocol
NEVPNProtocolIPSec *p = [[NEVPNProtocolIPSec alloc] init];
p.username = @"username";
p.serverAddress = @"serveraddress";
// get password persistent reference from keychain
NSString *password = @"thepassword";
NSData *paswordData = [password dataUsingEncoding:NSUTF8StringEncoding];
p.passwordReference = paswordData;
// PSK
p.authenticationMethod = NEVPNIKEAuthenticationMethodSharedSecret;
NSString *secret = @"123654987";
NSData *secretData = [secret dataUsingEncoding:NSUTF8StringEncoding];
p.sharedSecretReference = secretData;
p.useExtendedAuthentication = NO;
p.disconnectOnSleep = NO;
vpnManager.protocol = p;
vpnManager.localizedDescription = @"[email protected]";
[vpnManager saveToPreferencesWithCompletionHandler:^(NSError *error) {
NSLog(@"Save config failed [%@]", error.localizedDescription);
}];
}];
}
static NSString * const serviceName = @"com.progserv.vpn_config";
-(NSData *)searchKeychainCopyMatching:(NSString *)identifier {
NSMutableDictionary *searchDictionary = [[NSMutableDictionary alloc] init];
NSData *encodedIdentifier = [identifier dataUsingEncoding:NSUTF8StringEncoding];
searchDictionary[(__bridge id)kSecClass] = (__bridge id)kSecClassGenericPassword;
searchDictionary[(__bridge id)kSecAttrGeneric] = encodedIdentifier;
searchDictionary[(__bridge id)kSecAttrAccount] = encodedIdentifier;
searchDictionary[(__bridge id)kSecAttrService] = serviceName;
searchDictionary[(__bridge id)kSecMatchLimit] = (__bridge id)kSecMatchLimitOne;
searchDictionary[(__bridge id)kSecReturnPersistentRef] = @YES;
CFTypeRef result = NULL;
SecItemCopyMatching((__bridge CFDictionaryRef)searchDictionary, &result);
return (__bridge_transfer NSData *)result;
}
- (IBAction)CreateVPN:(id)sender {
NSError *startError;
[vpnManager.connection startVPNTunnelAndReturnError:&startError];
if (startError) {
NSLog(@"Start VPN failed: [%@]", startError.localizedDescription);
}
}
Upvotes: 2
Views: 1322
Reputation: 888
If loadFromPreferencesWithCompletionHandler
is never called, you probably haven't yet created an app-specific provisioning profile w/ VPN services enabled in Apple's developer portal... I'm sure you've seen this article, but the provisioning profile piece is very important: http://ramezanpour.net/post/2014/08/03/configure-and-manage-vpn-connections-programmatically-in-ios-8/
Upvotes: 1