【发布时间】:2014-11-25 01:22:55
【问题描述】:
VPN 连接无法通过代码工作。 为什么连不上服务器? 代码是:
- (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 = @"vpnusers@dyndns.org";
[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);
}
}
【问题讨论】:
-
loadFromPreferencesWithCompletionHandler的完成处理程序是否被调用?我的从来没有被调用过。
标签: ios objective-c vpn