Entering username and password for Alamofire request

爷,独闯天下 提交于 2019-12-07 17:50:26

问题


I'm trying to use Alamofire to do a GET request for my iOS app, but I don't really know how to enter my username and password.

The cURL command that ran successfully was

curl -k -u user:pw https://url/cli/method

and it returned the correct JSON

for Alamofire I tried

Alamofire.request(.GET, self.baseURL + method, parameters: params).responseJSON { (_, _, JSON, _) in
            println(JSON)

where baseURL + method is https://url/rest/method

and params is

let params = [
            "username" : self.username.text,
            "password" : self.password.text
        ]

but when I try it it says that

NSURLConnection/CFURLConnection HTTP load failed (kCFStreamErrorDomainSSL, -9813)

What am I doing incorrectly?


回答1:


Apparently there is a thing for this in Alamofire. I need to do...

Alamofire.request(.GET, self.baseURL + method, parameters: nil)
    .authenticate(user: self.username.text, password: self.password.text)
    .responseJSON { (_, _, JSON, _) in
        println(JSON) 
}

To authenticate myself.




回答2:


This error occurs when there is a problem with the certificate. My bets are that it is invalid. Unfortunately, Alamofire doesn't support overriding invalid certificates at the moment (like AFNetworking does for example).

Take a look at this thread for more details.



来源:https://stackoverflow.com/questions/31434008/entering-username-and-password-for-alamofire-request

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!