我正在尝试安装配置文件以使用 Swift 创建 VPN 连接。它创建了一个 VPN 连接,但我收到以下错误,我无法使用该 VPN 连接进行连接。
SecItemCopyMatching 失败:-50
钥匙串保存检索数据存在一些问题,但无法准确获取。
这是代码:
installVPNProfile(){
let server: String? = "127.0.0.1"
let username: String? = "MyServer"
var _: String? = ""
var _: String? = ""
// Save password & psk
//var password_Data = KeyChain.stringToNSDATA(string:"password")
keychain.set(("12345").data(using: .utf8)!, forKey: "VPN_PASSWORD")
keychain.set(("12345abcde").data(using: .utf8)!, forKey: "PSK")
vpnManager.loadFromPreferences(completionHandler: {(_ error: Error?) -> Void in
if error != nil {
print("Load config failed [\(String(describing: error?.localizedDescription))]")
return
}
var p: NEVPNProtocolIPSec? = (vpnManager.protocolConfiguration as? NEVPNProtocolIPSec)
if p != nil {
// Protocol exists.
// If you don't want to edit it, just return here.
}
else {
// create a new one.
p = NEVPNProtocolIPSec()
}
// config IPSec protocol
p?.username = username
p?.serverAddress = server
p?.passwordReference = self.keychain.getData("VPN_PASSWORD") // PSK
p?.authenticationMethod = authentication type
p?.sharedSecretReference = self.keychain.getData("PSK")
p?.useExtendedAuthentication = true
p?.disconnectOnSleep = false
self.vpnManager.protocolConfiguration = p
self.vpnManager.localizedDescription = "VPN Demo"
self.vpnManager.isEnabled = true
self.vpnManager.saveToPreferences(completionHandler: {(_ error: Error?) -> Void in
if error != nil {
print("Save config failed [\(error?.localizedDescription)]")
}
})
})
}