iOS 8 VPN 从代码连接,如果我没有证书
<p><p>VPN 连接无法通过代码工作。
为什么连不上服务器?
代码是:</p>
<pre><code>- (void)viewDidLoad {
;
// init VPN manager
self.vpnManager = ;
// 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 = [ init];
p.username = @"username";
p.serverAddress = @"serveraddress";
// get password persistent reference from keychain
NSString *password = @"thepassword";
NSData *paswordData = ;
p.passwordReference = paswordData;
// PSK
p.authenticationMethod = NEVPNIKEAuthenticationMethodSharedSecret;
NSString *secret = @"123654987";
NSData *secretData = ;
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 = [ init];
NSData *encodedIdentifier = ;
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;
;
if (startError) {
NSLog(@"Start VPN failed: [%@]", startError.localizedDescription);
}
}
</code></pre></p>
<br><hr><h1><strong>Best Answer-推荐答案</ strong></h1><br>
<p><p>如果 <code>loadFromPreferencesWithCompletionHandler</code> 从未被调用,您可能还没有在 Apple 的开发者门户中创建一个特定于应用程序的配置文件(其中启用了 VPN 服务)...我相信您已经看到了文章,但配置文件非常重要:<a href="http://ramezanpour.net/post/2014/08/03/configure-and-manage-vpn-connections-programmatically-in-ios-8/" rel="noreferrer noopener nofollow">http://ramezanpour.net/post/2014/08/03/configure-and-manage-vpn-connections-programmatically-in-ios-8/</a> </p></p>
<p style="font-size: 20px;">关于iOS 8 VPN 从代码连接,如果我没有证书,我们在Stack Overflow上找到一个类似的问题:
<a href="https://stackoverflow.com/questions/26120425/" rel="noreferrer noopener nofollow" style="color: red;">
https://stackoverflow.com/questions/26120425/
</a>
</p>
页:
[1]