gpt4 book ai didi

iOS 8 VPN 从代码连接,如果我没有证书

转载 作者:塔克拉玛干 更新时间:2023-11-02 21:20:20 30 4
gpt4 key购买 nike

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 从未被调用,您可能还没有在 Apple 的开发者门户中创建带有 VPN 服务的特定应用程序配置文件...我相信您已经看到了文章,但配置文件非常重要:http://ramezanpour.net/post/2014/08/03/configure-and-manage-vpn-connections-programmatically-in-ios-8/

关于iOS 8 VPN 从代码连接,如果我没有证书,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26120425/

30 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com