【问题标题】:iOS 8 VPN connect from code, if i have no certiOS 8 VPN 从代码连接,如果我没有证书
【发布时间】: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


【解决方案1】:

如果从未调用过loadFromPreferencesWithCompletionHandler,则您可能尚未在 Apple 的开发人员门户中创建一个启用 VPN 服务的应用特定配置文件...我相信您已经看过这篇文章,但配置文件个人资料很重要:http://ramezanpour.net/post/2014/08/03/configure-and-manage-vpn-connections-programmatically-in-ios-8/

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-07-18
    • 2016-06-11
    • 2014-09-18
    • 2013-09-11
    • 2017-10-25
    • 2021-09-28
    相关资源
    最近更新 更多