0

VPN 在 iOS 和 macOS 原生应用程序上成功连接,但在 mac Catalyst 上不工作。在控制台中出现错误,即返回 SecKeychainItemCopyContent 无法检索该项目的内容。 错误截图

VPNKeychain 参考: https ://developer.apple.com/forums/thread/84194

注意:我已经在功能选项卡中启用了个人 VPN 和钥匙串共享。

下面是我的代码-

VPN.shared.loadPreferences(userName: "someuser", password: "cankcajca", serverIP: "someIP", secret: "kjnkjcakjncka") { (success, errorMessage) in
            print("VPN callback")
            VPN.shared.connectVPN { (success) in
                print("connected")
            }
        }



    public func loadPreferences(userName: String, password: String, serverIP: String, secret: String, completionHandler: ((Bool, String?) -> Void)?) {
        self.userName = userName
        self.password = password
        self.serverIP = serverIP
        self.secret = secret
        
        self.vpnManager.loadFromPreferences {[weak self] (error) in
            guard let `self` = self else {
                completionHandler?(false, nil)
                return
            }
            
            if let tempError = error{
                completionHandler?(false, tempError.localizedDescription)
            }else{
                let vpnProtocol = NEVPNProtocolIPSec()
                vpnProtocol.username = self.userName
                vpnProtocol.serverAddress = self.serverIP
                vpnProtocol.authenticationMethod = NEVPNIKEAuthenticationMethod.sharedSecret
                if let secretRef = try? VPNKeychain.persistentReferenceFor(service: "VPN_Service2", account: "VPN_SECRET2", password: self.secret.data(using: .utf8)!){
                    vpnProtocol.sharedSecretReference = secretRef
                }
                
                if let passRef = try? VPNKeychain.persistentReferenceFor(service: "VPN_Service2", account: "VPN_PASSWORD2", password: self.password.data(using: .utf8)!){
                    vpnProtocol.passwordReference = passRef
                }
                
                vpnProtocol.useExtendedAuthentication = true
                vpnProtocol.disconnectOnSleep = false
                self.vpnManager.protocolConfiguration = vpnProtocol
                self.vpnManager.localizedDescription = "WH_VPN"
                self.vpnManager.isEnabled = true
                self.vpnManager.saveToPreferences {(error) in
                    if let tempError = error{
                        completionHandler?(false, tempError.localizedDescription)
                    }else{
                        completionHandler?(true, nil)
                    }
                }
            }
        }
    }
    
    public func connectVPN(completionHandler: ((Bool) -> Void)?) {
        if vpnManager.connection.status == .connecting || vpnManager.connection.status == .connected  { return }
        
        self.connectHandler = completionHandler
        do {
            try self.vpnManager.connection.startVPNTunnel()
        } catch {
            print("connectVPN failed: \(error.localizedDescription)")
        }
    }
4

0 回答 0