2

我能够保存 VPN 配置。请告诉我如何使服务器地址变量或获取服务器地址以及远程标识符是什么?

我已将此示例用作参考:Connect to a VPN with certificate - iOS/Swift

我该如何解决这个问题?

      func connectVPN(){
        do {
        if let file = URL(string: "example.com") {
                let data = try Data(contentsOf: file)
                let json = try JSONSerialization.jsonObject(with: data, options: [])
                if let object = json as? [String: String] {
                    // json is a dictionary
                    var data_VPN = object["VPN_data"]!
                    //print("Donebro:\(data_VPN)")
                    let certificate = data_VPN
                    let nsdata = certificate.data(using: .utf8)

                    let base64EncodedData = nsdata!.base64EncodedData()

                    print("base64StoreData:\(nsdata!)")
                    print("base64StoreNewData:\(base64EncodedData)")

                    var vpnManager = NEVPNManager.shared()

                                        vpnManager.loadFromPreferences { error in

                                            if vpnManager.`protocol` == nil{
                                                let newIPSec = NEVPNProtocolIPSec()

                                                newIPSec.serverAddress = ""
                                                newIPSec.localIdentifier = ""
                                                newIPSec.remoteIdentifier = ""
                                                newIPSec.useExtendedAuthentication = true
                                                newIPSec.identityData = base64EncodedData as! Data
                                                newIPSec.authenticationMethod = NEVPNIKEAuthenticationMethod.certificate
                                                print("VPNDATA:\(newIPSec)")

                                                if #available(iOS 9, *) {
                                                vpnManager.protocolConfiguration = newIPSec
                                                } else {
                                                vpnManager.`protocol` = newIPSec
                                                }

                                                vpnManager.isEnabled = true
                                                vpnManager.saveToPreferences(completionHandler: { (error) -> Void in
                                                    if ((error) != nil) {
                                                        print("VPN Preferences error: 2")
                                                    }
                                                    else {
                                                        vpnManager.loadFromPreferences(completionHandler: { (error) in
                                                            if ((error) != nil) {
                                                                print("VPN Preferences error: 2")
                                                            }
                                                            else {
                                                                var startError: NSError?
                                                                do {
                                                                    try vpnManager.connection.startVPNTunnel()
                                                                }
                                                                catch let error as NSError {
                                                                    startError = error
                                                                    print(startError)
                                                                }
                                                                catch {
                                                                    print("Fatal Error")
                                                                    fatalError()
                                                                }
                                                                if ((startError) != nil) {
                                                                    print("VPN Preferences error: 3")
                                                                    let alertController = UIAlertController( title: "Oops..", message: "Something went wrong while connecting to the VPN. Please try again.", preferredStyle: UIAlertControllerStyle.alert)
                                                                    alertController.addAction( UIAlertAction(title: "Dismiss", style: UIAlertActionStyle.default,handler: nil))
                                                                    self.present(alertController, animated: true, completion: nil)
                                                                    print(startError)
                                                                }
                                                                else {
                                                                    //VPNStatusDidChange(nil)
                                                                    print("Start VPN")
                                                                }
                                                            }
                                                        })
                                                    }
                                                })
                    }

                } else if let object = json as? [Any] {
                    // json is an array
                    for anItem in object as! [Dictionary<String, AnyObject>] {
                        let industryName = anItem["VPN_data"] as! String                                               
                    }
                } else {
                    print("JSON is invalid")
                }
            } else {
                print("no file")
            }
        } catch {
            print(error.localizedDescription)
        }

    }
}
4

0 回答 0