POST请求使用Swift 3接受自签名证书

时间:2016-12-01 15:23:09

标签: swift syntax certificate swift3 self-signed

我尝试过以下许多解决方案,允许我的应用程序使用 Swift 3 连接并忽略自签名证书但没有成功。

https://stackoverflow.com/a/30820452/7126704

我的连接登录功能:

func Login(_ user: String, psw: String){
    loading = MBProgressHUD.showAdded(to: self.view, animated: true)
    loading.isUserInteractionEnabled = false
    self.view.isUserInteractionEnabled = false

    Accedi.isEnabled = false

    let userPost = "&username=" + user
    let passPost = "&password=" + md5(string: psw)
    let postString = "func=LOGIN" + userPost + passPost + "&appver=" + getAppVersion()

    let request = NSMutableURLRequest(url: URL(string: ServerPath)!)
    request.httpMethod = "POST"

    request.httpBody = postString.data(using: String.Encoding.utf8)
    request.timeoutInterval = 10

    let configuration = URLSessionConfiguration.default

    let session = URLSession(configuration: configuration, delegate: self, delegateQueue: OperationQueue.main)

    let task = session.dataTask(with: request as URLRequest, completionHandler: { (data, response, error) in

        guard error == nil && data != nil else {
            let getError:NSString = (error?.localizedDescription)! as NSString
            print("[Login] Error = \(getError)")

            if ((self.usernameField.text! != "") || (self.passwordField.text! != "")){
                self.Accedi.isEnabled = true
            }else{
                self.Accedi.isEnabled = false
            }

            DispatchQueue.main.async {
                loading.hide(animated: true)
                self.view.isUserInteractionEnabled = true
                let alert = UIAlertController(title: sError, message: sConnError + ": \(getError)", preferredStyle: UIAlertControllerStyle.alert)
                alert.view.tintColor = globalColor
                alert.addAction(UIAlertAction(title: sOk, style: UIAlertActionStyle.default, handler: nil))
                self.present(alert, animated: true, completion: nil)
            }
            return
        } [...]

我可以使用什么解决方案来解决这个问题?一个小例子可以帮助我和其他人。

0 个答案:

没有答案