首页 > 解决方案 > 连接 VPN IKEv2 它可以在 iOS 平台上工作,但 macOS 无法工作,面临同样的问题

问题描述

我在 macOS Development 上连接 IKEv2 协议时遇到问题,程序在断开连接时工作正常但是当我进行连接时需要继续设置连接

我正在使用 IKEv2 协议进行 VPN 连接,使用 NetworkExtention 文件,

public func connectIKEv2(config: Configuration, onError: @escaping (String)->Void) {
    let p = NEVPNProtocolIKEv2()

    if config.pskEnabled {
        p.authenticationMethod = NEVPNIKEAuthenticationMethod.sharedSecret
    } else {
        p.authenticationMethod = NEVPNIKEAuthenticationMethod.none
    }
    p.serverAddress = config.server
    p.disconnectOnSleep = false
    p.deadPeerDetectionRate = NEVPNIKEv2DeadPeerDetectionRate.medium
    p.username = config.account
    p.passwordReference = config.getPasswordRef()
    p.sharedSecretReference = config.getPSKRef()
    p.disableMOBIKE = false

    p.disableRedirect = false
    p.enableRevocationCheck = false
    p.enablePFS = false
    p.useExtendedAuthentication = true
    p.useConfigurationAttributeInternalIPSubnet = false

    // two lines bellow may depend of your server configuration
    p.remoteIdentifier = config.server
    p.remoteIdentifier = config.remote
    p.localIdentifier = config.account

    loadProfile { _ in
        self.manager.protocolConfiguration = p
        if config.onDemand {
            self.manager.onDemandRules = [NEOnDemandRuleConnect()]
            self.manager.isOnDemandEnabled = true
        }

        self.manager.isEnabled = true

        self.saveProfile { success in
            if !success {
                onError("Unable to save vpn profile")
                return
            }
            self.loadProfile() { success in
                if !success {
                    onError("Unable to load profile,")
                    return
                }
                let result = self.startVPNTunnel()
                print("Show Result, \(result)")
                if !result {
                    onError("Can't connect")
                }
            }
        }
    }
}
private func startVPNTunnel() -> Bool {
    do {
        try self.manager.connection.startVPNTunnel()
        return true
    } catch NEVPNError.configurationInvalid {
        NSLog("Failed to start tunnel (configuration invalid)")
    } catch NEVPNError.configurationDisabled {
        NSLog("Failed to start tunnel (configuration disabled)")
    } catch {
        NSLog("Failed to start tunnel (other error)")
    }
    return true
}

我正面临 macOS 开发的错误,

open(/var/db/DetachedSignatures) - 未定义错误:0 连接错误:无法加载配置文件,

但是一些关于iOS开发的代码工作文件,

标签: iosswiftswift4swift4.2

解决方案


推荐阅读