OGeek|极客世界-中国程序员成长平台

标题: iOS 8 VPN 从代码连接,如果我没有证书 [打印本页]

作者: 菜鸟教程小白    时间: 2022-12-13 02:17
标题: iOS 8 VPN 从代码连接,如果我没有证书

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 = @"[email protected]";

        [vpnManager saveToPreferencesWithCompletionHandler:^(NSError *error) {
            NSLog(@"Save config failed [%@]", error.localizedDescription);
        }];

    }];

   }

static NSString * const serviceName = @"com.progserv.vpn_config";

-(NSData *)searchKeychainCopyMatchingNSString *)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)CreateVPNid)sender {
    NSError *startError;
    [vpnManager.connection startVPNTunnelAndReturnError:&startError];


    if (startError) {
        NSLog(@"Start VPN failed: [%@]", startError.localizedDescription);
    }
}



Best Answer-推荐答案


如果 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/






欢迎光临 OGeek|极客世界-中国程序员成长平台 (http://sqlite.in/) Powered by Discuz! X3.4